[Explained] When is an expression not an expression?
3 direct replies — Read more / Contribute
|
by davies
on Jun 03, 2026 at 13:26
|
C:\Windows\system32>perl -E "my @rray = qw(a b c); my $rx = qr(d); say
+ grep $rx, @rray"
abc
C:\Windows\system32>perl -E "my @rray = qw(a b c); say grep /d/, @rray
+"
I was trying to see if an array contained an element. I started by using the upper format and my code did not work. This was unexpected, because the perldoc for grep says:
grep EXPR,LIST
This is similar in spirit to, but not the same as, grep(1) and its relatives. In particular, it is not limited to using regular expressions.
Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value consisting of those elements for which the expression evaluated to true. In scalar context, returns the number of times the expression was true.
To me, this meant that both forms should have worked. Why should I have read it as meaning that the first form should fail and the second work?
Regards,
John Davies
|