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

Re: or or

by ahunter (Monk)
on Jul 08, 2000 at 15:53 UTC ( #21645=note: print w/ replies, xml ) Need Help??


in reply to or or

They differ in precedence, and are otherwise the same. This means that if you write:

$a=$b || $c; $a=$b or $c;
Perl evaluates this:
$a=($b || $c); ($a=$b) or $c;
This changes what gets assigned to $a, and can also change the context of a statement (from perlop):
@info = stat($file) || die; # oops, scalar sense of stat! @info = stat($file) or die; # better, now @info gets its due
This occurs because die is in scalar context, (so || is great for obfuscation!). || is also appropriate where you want to cause the rightmost value to get assigned. That is:
$a=$b || $c
Would assign $c to $a if $b was false, otherwise it would assign $b to $a. Whereas:
$a=$b or $c
Would not assign $c to $a when $b was false! In scalar context, using || for dieing is fine, but you'll run into trouble with things where context is important. or is almost always what you mean in these cases.

If you still don't believe me, try this:

$a=0 or 1; $b=0 || 1;
$a will be 0, and $b will be 1.

Andrew.




Comment on Re: or or
Select or Download Code
RE: Re: or or
by Aighearach on Jul 09, 2000 at 00:51 UTC
    >or is almost always what you mean in these cases.

    This is largely a matter of programming style, and the nature of the project, not something inherent to Perl. I find myself using || about ten times as often as or.

    Because most of my program data is non-zero, I find myself using a lot of things like:

    $param = $value || $default;

    OTOH, maybe I am using too many if/else blocks...

     

    Paris Sinclair    |    4a75737420416e6f74686572
    pariss@efn.org    |    205065726c204861636b6572
    I wear my Geek Code on my finger.
    
      I'm not sure what you mean here - you provide a classic example of where or is most certainly not appropriate (as if you use it, $param will never get the value of $default).

      That is,

      $param = $value or $default;
      Will result in $param always getting the value of $value, never $default. or has lower precedence than = (in fact it has the absolute lowest precedence of all the operators). Maybe it was just me not being clear, though. You should always use 'or' where what you really want to say is:
      ($param=$value) or die "darn"
      That is, you don't want the value of die to get assigned to $param (yeah, die doesn't return very often. But it forces scalar context, which is why the alternative is a Bad Thing). Using or, your example would have to be:
      $param=$value or $param=$default
      So it's not always a matter of style.

      Andrew.

        I don't think you understood my point. My point was, TMTOWTDI, so depending on which stucures you build your program with, one or the other will be more commonly useful.

        Sorry to have been so unclear.

        But I'm not sure what your point is about the code, yes I gave an example that is not appropriate with or, it illustrates that your algorithm logic is the determining factor in which operator is more common.

         

        Paris Sinclair    |    4a75737420416e6f74686572
        pariss@efn.org    |    205065726c204861636b6572
        I wear my Geek Code on my finger.
        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (17)
As of 2014-07-30 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (236 votes), past polls