Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

grep {CONSTANT} @list

by creeble (Sexton)
on Nov 21, 2012 at 18:52 UTC ( #1004985=perlquestion: print w/replies, xml ) Need Help??
creeble has asked for the wisdom of the Perl Monks concerning the following question:

Sometimes you just want to scream. ugh.
use constant REGEXP => qr(string); @list = ('astring', 'notstriing'); $s = 'bstring'; print $s =~ REGEXP; # true print grep {REGEXP} @list; # prints whole list. print grep {/string/} @list; # prints correct match.
I know there's a good reason having to do with the definition of grep, but I mean *really*.

Replies are listed 'Best First'.
Re: grep {CONSTANT} @list
by tobyink (Abbot) on Nov 21, 2012 at 20:08 UTC

    Nothing to do with grep in particular, or constants.

    use constant REGEXP => qr(a); print grep { REGEXP } qw(a b c);

    ... is equivalent to:

    my @tmp; foreach (qw(a b c)) { push @tmp, $_ if qr(a); } print @tmp;

    See the problem now? if qr(a) is always true. if qr(anything) is always true.


    print grep { $_ =~ REGEXP } @list;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: grep {CONSTANT} @list
by LanX (Chancellor) on Nov 21, 2012 at 20:42 UTC
    you are confusing a regex data-type with a regex operation.

    grep {REGEXP} uses a code-block which returns a regex which is always true if defined.

    grep {/string/} uses a m// (match operation) which defaults to test $_.

    Cheers Rolf

Re: grep {CONSTANT} @list
by afoken (Abbot) on Nov 21, 2012 at 19:05 UTC

    Documented in constant. Documented better (and fixed) in Readonly.


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      I don't see this documented in constant -- or at least, there's no mention of qr, grep, or regexp on the constant manpage.
        don't look for keywords, read docs, esp the notes and caveats
Re: grep {CONSTANT} @list
by frozenwithjoy (Priest) on Nov 21, 2012 at 19:52 UTC

    The way I see it, you still need to put // around the pattern you are trying to match that is contained in REGEX (in this case it is (?^:string)).

    say grep { REGEX } @list; #doesn't work, because it isn't really doing what you think it is doin +g say grep { /REGEX/ } @list; #doesn't work because of the problems mentioned above when using const +ants in contexts that auto-quote barewords say grep { /(?^:string)/ } @list; #this works (and throws errors if // are removed) use Readonly; Readonly::Scalar my $regex_ro => qr/string/; say grep {/$regex_ro/} @list; #this works (and, as expected, gives same bad result you got if // is +removed) my $regex = qr/string/; say grep { /$regex/ } @list; #this works (and, as expected, gives same bad result you got if // is +removed)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1004985]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2017-06-28 21:13 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (648 votes). Check out past polls.