While you are correct, I like to keep to what I think are best practices in my regular coding.

Of course, you shall keep on on your best practices. But there are more best practices than only one. One is to program very efficient, the other is to guarantee a simple maintainance and readability. Both ways are there for avoiding problems in the future, but there is no best best practice.

I would have also used the /^(...|...|...)$/ way. My most important reason would have been, that it is easier to read. The extra ?: doesn't have an influence to the algorithm done by the program, it only influences the internal way Perl is handling the regexp and its global variables. But the two extra character are confusing our eyes, making it a bit (really only a bit, but why should I renounce it) more difficulty to understand the algorithm some years later.

That doesn't mean, you're on the the false way, I only wanted to clarify that it is correct to give a beginner a simple, but (still) productive way, unless the speed penalty really hurts.

You gave another excellent example:
my $mth = ( split(/\s+/, localtime) )1;
I would have written it either as:
use POSIX qw/strftime/;
my $month = strftime "%m", localtime;
or
use Date::Simple;
my $today = Date::Simple->new;
my $month = $today->month;
allthough both ways are much slower than your suggested one. Both have the benefit that you don't need to know what the magic 1 is referencing in the time array to (what can be a great benefit if some other or myself in one year) has to maintain the code. (In addition you can avoid extra calculations to the month as they range in the natural 01-12 or 1-12 way and not from 0..11)

Greetings,
Janek

In reply to Re: Re: Re: Re: Re: Code review: validation regexes by bigj
in thread Code review: validation regexes by Anonymous Monk

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.