Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

perl regular expressions and empty strings unexpected results

by bdimych (Monk)
on Jan 23, 2012 at 09:39 UTC ( #949344=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://949344]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2023-09-26 07:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?