http://www.perlmonks.org?node_id=976837


in reply to Re^2: Text matching repost
in thread Text matching repost

I could just as well use (and more efficient too):
if ($&) { print; }

Ummm, no. $& is one of the "ugly three" variables (the other two are $` and $') that kill performance. From perlvar:

The use of this variable anywhere in a program imposes a considerable performance penalty on all regular expression matches. To avoid this penalty, you can extract the same substring by using @-. Starting with Perl 5.10, you can use the /p match flag and the ${^MATCH} variable to do the same thing for particular match operations.

Apart from that, it may work for this special problem, but it does not work if $& evaluates to false:

perl -E '$x="a0b"; $x=~/0/; say $&; if ($&) { die "not reached!" } if + ($x=~/0/) { say "matched zero" }'

Output:

0 matched zero

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^4: Text matching repost
by Neighbour (Friar) on Jun 19, 2012 at 06:39 UTC
    Good call, it shall be
    if (scalar @-) { print; }
    then :)