Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by flounder99 (Friar)
on Sep 16, 2003 at 12:08 UTC ( [id://291794]=note: print w/replies, xml ) Need Help??


in reply to Re: To || or not to or and why.
in thread To || or not to or and why.

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

Replies are listed 'Best First'.
Re: Re^2: To || or not to or and why.
by krisahoch (Deacon) on Sep 16, 2003 at 16:09 UTC

    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://291794]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found