Archive for February, 2003

Non-Greedy Regular Expressions

Wednesday, February 12th, 2003

Regular expressions in Perl are "greedy." That means that if you use a * or + operator in a regular expression, it grabs as much of the string as it can. This can be frustrating at times, but it’s useful in other respects. Consider this:

  my $ip_addr = "192.168.1.2";
  my ($network, $host) = ($ip_addr =~ /(.+)\.(.+)/);
  print "network=$network host=$host\n";

You need a way to know for sure whether $network gets "192.168.1" and $lastpart gets "2" or whether the split is "192" vs. "168.1.2". The decision was made to have it be "greedy" which means that the first + grabs the lion’s share, and the second one gets the leftovers. Put another way, the first one gets as much as possible short of making it impossible to match the string.

(more…)

Constants Again

Wednesday, February 12th, 2003

Do you ever write programs that use constants? You probably do without knowing it. The canonical example is a geometry program that uses the special number π (pi):

  my $area = 2 * 3.14159 * $radius;

But what if you have multiple parts of your program where you have to put the value, and maybe you mistype it in one place, or use the wrong number of digits? So you put it in a variable at the top of your program:

  my $pi = 3.14159;
  ...
  my $area = 2 * $pi * $radius;

That’s fine. But what if you accidentally modify the value of $pi? It’s not so hard as you think. Consider this buggy line of code. The programmer wanted to compare the value of $pi to the formula, but used = instead of == by mistake:

  if ($pi = $x / ( 2 * $radius ))
  {
      print "$x is the area\n";
  }

(more…)