I'm trying to pass a precompiled regular expression to grep(), but instead of properly matching, grep returns the entire array. Putting the precompiled regex in // produces the desired behavior. Seen in 5.6.1 and 5.8.0.

Here's a sample testcase:
#!/usr/bin/perl -w use strict; use Test::More tests => 3; use Test::Differences; my @input = qw(nope skip uh-uh no yes stop yup not-me groovy wrong ok +ay); my @output = qw(yes yup groovy okay); my $re = qr/y/; eq_or_diff( [ grep( /y/, @input ) ], \@output, "Embedded RE" ); eq_or_diff( [ grep( $re, @input ) ], \@output, "Compiled, passed RE" + ); eq_or_diff( [ grep( /$re/, @input ) ], \@output, "Compiled, but slashe +d RE" );
Test 2 fails; grep has returned the whole input array:
grep_re....1..3
ok 1 - Embedded RE
not ok 2 - Compiled, passed RE
#     Failed test (grep_re.t at line 13)
# +----+--------+----+----------+
# | Elt|Got     | Elt|Expected  |
# +----+--------+----+----------+
# *   0|nope    *    |          |
# *   1|skip    *    |          |
# *   2|uh-uh   *    |          |
# *   3|no      *    |          |
# |   4|yes     |   0|yes       |
# *   5|stop    *    |          |
# |   6|yup     |   1|yup       |
# *   7|not-me  *    |          |
# |   8|groovy  |   2|groovy    |
# *   9|wrong   *    |          |
# |  10|okay    |   3|okay      |
# +----+--------+----+----------+
ok 3 - Compiled, but slashed RE
# Looks like you failed 1 tests of 3.
dubious
        Test returned status 1 (wstat 256, 0x100)
DIED. FAILED test 2
        Failed 1/3 tests, 66.67% okay
Failed Test Stat Wstat Total Fail  Failed  List of Failed
-------------------------------------------------------------------------------
grep_re.t      1   256     3    1  33.33%  2
Failed 1/1 test scripts, 0.00% okay. 1/3 subtests failed, 66.67% okay.
Has anyone else experienced this?

Update: Changed title to be more clear.


--isotope

In reply to Grep matches all elements if compiled regex is passed as EXPR by isotope

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.