Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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.

Replies are listed 'Best First'.
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.


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.

    local $_ = "0A72656B636148206C72655020726568746F6E41207473754A"; while(s/..$//) { print chr(hex($&)) }

Log In?

What's my password?
Create A New User
Domain Nodelet?
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 taking refuge in the Monastery: (2)
As of 2022-05-20 14:11 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (73 votes). Check out past polls.