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

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.

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

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.

    Andrew.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2014-07-14 05:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (255 votes), past polls