Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Grouping Symbols?

by Kozz (Friar)
on Jul 08, 2000 at 18:52 UTC ( #21652=note: print w/ replies, xml ) Need Help??

in reply to or or

Or that's what we call them in mathematics. If you use parentheses around the arguments of a function, I think this would remove any concern over "or" vs. "||".
Like this:

open(FH, "filename.txt") || die "$!"; # OR open(FH, "filename.txt") or die "$!";
Functionality should be identical in the two cases above, no? Of course, this is only for calling of functions or something like that. It doesn't work the same with other above-mentioned examples such as
$a = $b || $c;
Which is an entirely different sort of thing.

Comment on Grouping Symbols?
Select or Download Code
RE: Grouping Symbols?
by reptile (Monk) on Jul 08, 2000 at 19:14 UTC

    AFAIK, the examples you gave still pose a problem. What if you were to assign the return value of open() to a variable:

    $foo = open(FH, "filename.txt") || die $!; # not the same as $foo = open(FH, "filename.txt") or die $!;

    but they appear to be the same anyway ;) It's not the parentheses that's doing it, it's just the way it works. It's checking to see if the return of open() is true, and if not, tries to assign the return value of die to $foo, which never happens because it dies.

    So it works the same either way, but it's actually doing two different things.


RE: Grouping Symbols?
by ahunter (Monk) on Jul 08, 2000 at 21:23 UTC
    You are right, the functionality is identical in the cases you cite. But || comes with a *lot* of caveats when you use it in circumstances like this. The RHS of or will always be executed last (unless parentheses are used, or there is more than one or). This means if you want to say (psuedo-code):
    if (open fails) { die }
    or gives you a way of writing literally that. || means something a bit different, and can cause considerable confusion. It's a bad habit and a maintainance problem to use the wrong operator for the job. See perlop.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-12-27 01:11 GMT
Find Nodes?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?

    Results (176 votes), past polls