http://qs321.pair.com?node_id=949344

bdimych has asked for the wisdom of the Perl Monks concerning the following question:

Hi
perl -e 'print ""=~/foo/ and ""=~/bar/'
prints nothing as expected
but
perl -e 'print ""=~/.*/ and ""=~/bar/'
prints 1
imho looks strange
I could not find something in documentation
why so?

Replies are listed 'Best First'.
Re: perl regular expressions and empty strings unexpected results
by moritz (Cardinal) on Jan 23, 2012 at 09:44 UTC
    and has very loose precedence, so your last example is parsed as
    (print "" =~ /.*/) and "" =~ /bar/

    The first regex match succeeds (thus the 1), and the result of the second one is discarded. Use && instead of and to get the result you want.

    See also: perlop.

Re: perl regular expressions and empty strings unexpected results
by Anonymous Monk on Jan 23, 2012 at 10:19 UTC
    Perl comes with B::Deparse
    $ perl -e " print ''=~/.*/ and ''=~/bar/ " 1 $ perl -MO=Deparse -e " print ''=~/.*/ and ''=~/bar/ " '' =~ /bar/ if print '' =~ /.*/; -e syntax OK $ perl -MO=Deparse,-p -e " print ''=~/.*/ and ''=~/bar/ " (print(('' =~ /.*/)) and ('' =~ /bar/)); -e syntax OK