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.


In reply to Re: Data normalisation by Ovid
in thread Data normalisation by Perl-chick

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.