Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by krisahoch (Deacon)
on Sep 16, 2003 at 16:09 UTC ( #291864=note: print w/replies, xml ) Need Help??


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

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; }

Replies are listed 'Best First'.
Re^4: To || or not to or and why.
by flounder99 (Friar) on Sep 17, 2003 at 20:54 UTC
    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://291864]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2022-05-29 04:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?