in reply to Data normalisation

I just wanted to respectfully disagree with ZZamboni's putting the capturing parentheses in the variable assignments as opposed to the regex. Generally, when you have a complicated code that is well-developed but has a general use, don't throw anything in there which is irrelevant to the purpose of that code.

In this particular case, you are simply defining $month, $day, and $year, as was done in the original regex. This is good. However, the actual capturing of the values is not done until the regex and the capturing parens should be listed in the regex, not in the variable assignment. Imagine what would happen the first time another programmer (or yourself, several years later) comes along and tries to reuse that code.

print $1 if /(${month}${day}${year})/io;
The above code would work, but what if someone just wants the year?
print $1 if /${month}${day}(${year})/io;
Oops! Now our code is broken because the capturing parentheses were put into the variable assignment rather than placed directly in the regex. This could be very confusing to debug.

The moral of this tale? Make each little snippet of code as generic as possible and when something needs to be done (such as capturing a value to $1), do it where it's being done. Don't hide it somewhere else.

As a side note, I noticed that everyone has dropped the curly braces {} from around the variables. While I will admit that the curly braces are not strictly necessary in the example, I like them for two reasons:

  1. If you are used to them, they make the variables stand out (IMHO)
  2. In complicated regexes using variable interpolation, they can help to guarantee that the regex compiler is examining the variable you are expecting, rather than pulling in extra characters into your variable name.
The last point may seem strange, but I've been bitten by this more than once. The compiler has given me warnings about using variables in the regex that haven't been declared. By putting them in the curly braces, all warnings went away. It's a habit for me now.

Incidentally: Perl-chick's code is from a response to this node.

Replies are listed 'Best First'.
RE: Re: Data normalisation
by ZZamboni (Curate) on Jul 06, 2000 at 21:17 UTC
    This is a very good point, Ovid. Sometimes the easiest solution may not be the most maintenable one. In fact, putting the grouping parenthesis in the regex instead of in the variable assignments only adds a little bit of postprocessing. You only need to remove the possible suffixes from $day. So something like this would work:
    if (( ($m, $d, $y) = (/($month)($day)($year)/io) ) || ( ($d, $m, $y) = (/($day)($month)($year)/io) ) ) { $y += 1900 if $y < 100; $m = substr($m, 1, 3); $d =~ s/^(\d+).*$/$1/; print "$d-$m-$y\n"; }
    Another thing I noticed is that we had been happily assigning the matching strings to the same variables used to hold the regular expressions. Not good. So I renamed the variables in this new code segment.

    --ZZamboni

      We had been happily assigning the matching strings to the same variables used to hold the regular expressions.

      ROTFL!!! I can't stop laughing over this one. Whups!! :)