in reply to Data normalisation
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.
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.print $1 if /${month}${day}(${year})/io;
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:
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 | |
by Ovid (Cardinal) on Jul 06, 2000 at 21:21 UTC |