Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

and vs &&

by marcussen (Pilgrim)
on Feb 12, 2009 at 00:56 UTC ( [id://743211]=perlquestion: print w/replies, xml ) Need Help??

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

This passed my desk today...

$ perl -e 'my $a=(1 == 1) && (0);print "$a\n";' 0 $ perl -e 'my $a=(1 == 1) and (0);print "$a\n";' 1 $ perl -e 'my $a=((1 == 1) and (0));print "$a\n";' 0
I must admit that I would have expected the result to be the same, yet it appears that the binary and forgets the boundaries of it's condition and always evaluates to true if the left hand condition is true while not encapsulated by parantheses to mark the boundary. Is my interpretation of perldoc perlop wrong or do I need more coolaid?

Confucius says kill mosquito unless cannon

Replies are listed 'Best First'.
Re: and vs &&
by kennethk (Abbot) on Feb 12, 2009 at 00:59 UTC
Re: and vs &&
by JavaFan (Canon) on Feb 12, 2009 at 01:17 UTC
    $ perl -we 'my $a=(1 == 1) and (0);print "$a\n";' Found = in conditional, should be == at -e line 1. 1
    Should be a hint...
Re: and vs &&
by ikegami (Patriarch) on Feb 12, 2009 at 03:31 UTC
    >perl -MO=Deparse,-p -e"my $a = $b and $c" ((my $a = $b) and $c); -e syntax OK
Re: and vs &&
by trwww (Priest) on Feb 12, 2009 at 03:53 UTC

    Hello,

    It is possible to write relatively obfuscated Perl. But perl will will be happy do de-obfuscate it for you if you ask it to:

    $ perl -e 'my $a=(1 == 1) && (0);print "$a\n";' 0 $ perl -MO=Deparse -e 'my $a=(1 == 1) && (0);print "$a\n";' my $a = 0; print "$a\n"; -e syntax OK $ perl -e 'my $a=(1 == 1) and (0);print "$a\n";' 1 $ perl -MO=Deparse -e 'my $a=(1 == 1) and (0);print "$a\n";' '???' if my $a = 1; print "$a\n"; -e syntax OK

    regards,

Re: and vs &&
by marcussen (Pilgrim) on Feb 12, 2009 at 01:00 UTC

    To eliminate doubts about the right hand condition evaluating to an anonymous list with one entry of the value 0, which evaluates to true I would like to append the following;

    $ perl -e 'my $a=(1 == 1) and 0;print "$a\n";' 1

    Confucius says kill mosquito unless cannon
      To eliminate doubts about the right hand condition evaluating to an anonymous list with one entry of the value 0....

      Where do you see a list in that code?

        I never saw a list, but as parentheses are also used to enforce list context someone might percieve (0) to be 0 in list context. I hope that explains my cryptic clarification :)

        Confucius says kill mosquito unless cannon
Re: and vs &&
by imrags (Monk) on Feb 12, 2009 at 08:34 UTC
    when i type this
    perl -e 'my $a=(1 == 1) && (0);print "$a\n";'
    it gives me error:
    print was unexpected at this time.
    I'm using perl 5.10.0 (activestate) on WinXP :( (yes, i use Windows with perl)
    Raghu
      your shell uses double-quotes to quote arguments.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-28 16:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found