The original syntax is correct ...

The original syntax may be correct in that it compiles without error, but it is semantically incorrect in that it doesn't actually do anything. grep looks at the truthiness of the evaluated expression or statement block, and a Regexp object alone is always true. Only when evaluated within a match operator can it result in a true or false value. Consider:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "use Regexp::Common qw /net/; ;; my $records = [ qw(1.2.3.4 12.23.34.45 9.9.9 x.x.x.x anything) ]; ;; my @temp = grep {$RE{net}{IPv4}} @$records; dd \@temp; ;; @temp = grep { m/$RE{net}{IPv4}/ } @$records; dd \@temp; " ["1.2.3.4", "12.23.34.45", "9.9.9", "x.x.x.x", "anything"] ["1.2.3.4", "12.23.34.45"]

Update: The code would also have been semantically correct had it used an explicit binding of  $_ to the Regexp object:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "use Regexp::Common qw /net/; ;; my $records = [ qw(1.2.3.4 12.23.34.45 9.9.9 x.x.x.x anything) ]; ;; my @temp = grep { $_ =~ $RE{net}{IPv4} } @$records; dd \@temp; " ["1.2.3.4", "12.23.34.45"]


Give a man a fish:  <%-{-{-{-<


In reply to Re^2: regex in perl by AnomalousMonk
in thread regex in perl by user786

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.