Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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
Node Status?
node history
Node Type: perlquestion [id://949344]
Approved by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2018-07-20 01:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (422 votes). Check out past polls.

    Notices?