Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: To || or not to or and why.

by gjb (Vicar)
on Sep 15, 2003 at 22:43 UTC ( [id://291675]=note: print w/replies, xml ) Need Help??


in reply to To || or not to or and why.

Abigail-II and dws have valid points you should take to heart, but don't answer your original question which is about the difference between or and ||. The difference is in precedence, || has higher precedence than or. This can be found in the perlop manpage.

Note: especially keep in mind the relative precende of or and || with respect to assignment.

Just my 2 cents, -gjb-

Replies are listed 'Best First'.
Re^2: To || or not to or and why.
by flounder99 (Friar) on Sep 16, 2003 at 12:08 UTC
    A good example of the difference in precedence of or and || is in the common perl idiom of
    open FILE, "file" or die;
    it is not the same as
    open FILE, "file" || die;
    since || has a higher precedence than open the "file" || die gets evaluated before the open. So it will not die even if the open fails. or has a lower precedence than open so the open gets evaluated first in the first example. To have the same effect using || you have to use
    open(FILE, "file") || die;

    --

    flounder

      Flounder,

      Thank you for that explaination. So if I am reading it right, then the would following be true?
       
      The following functions set the value of the return to 'default' by default. Then if was a parameter passed, $value gets set to that parameter. The differences between the two are:
      1. The juxteposition of the shift() statement and 'default'
      2. The use of the '||' and 'or' operators
      sub setValue_takeOne{ my $value = shift() || 'default'; return $value; } sub setValue_takeTwo{ my $value = 'default' or shift(); return $value; }

        No.
        my $value = shift() || 'default';
        and
        my $value = shift() or 'default';
        will not work the same.
        my $value = shift() || 'default';
        will set $value to the value returned by shift() if it is true and to 'default' if it is false.
        my $value = shift() or 'default';
        will set $value to the value returned by shift() no matter what!
        Here is an example using a handy tool B::Deparse that will show you how the compiler read your program, the -p adds extra parentheses to make it very clear.
        #temp.pl $value = $x or $y or $z; $value = $x || $y || $z; $value = $x or $y || $z; $value = $x || $y or $z;
        then run
        perl -MO=Deparse,-p temp.pl
        it outputs
        ((($value = $x) or $y) or $z); ($value = (($x || $y) || $z)); (($value = $x) or ($y or $z)); (($value = ($x || $y)) or $z); temp.pl syntax OK
        if you notice
        $value = $x or $y or $z;
        and
        $value = $x or $y || $z;
        just set $value to the value of $x no matter what the value of $y or $z because or has a lower precidence than =.
        Did you notice that
        $value = $x || $y or $z;
        will just set $value to the value of $x || $y no matter what the value of $z?

        --

        flounder

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-24 09:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found