import-bot (20211) [Avatar] Offline
#1
[Originally posted by lsanger]

I see some people write "or die" and others write "|| die".

Is there reason to prefer one? You write both in your book but seem to prefer
the latter.

Also, how would "A and B || C and D" be parsed?

My guess is "(A and ((B or C) and D))"...

Thanks, as always, for this great forum and the help!

Larry Sanger
import-bot (20211) [Avatar] Offline
#2
Re: "or die"...
[Originally posted by jandrew]

Hi Larry,

> I see some people write "or die" and others write "|| die".
>
> Is there reason to prefer one? You write both in your book but seem
> to prefer the latter.

The difference is precedence: '||' is high precedence and 'or' is low
precedence. With the 'or' version you can write:

open FILE, 'filename' or die "Can't: $!";

You couldn't use || there unless you parenthesized the arguments to
the open call:

open(FILE, 'filename') || die "Can't: $!";

I do tend to favor the latter form in the case of open() calls, but
will often use the 'or' form when the left hand side is an open
expression rather than a function call:

$p + $q <= 1 or die "Probabilities ($p + $q) sum to more than 1.0";


> Also, how would "A and B || C and D" be parsed?
>
> My guess is "(A and ((B or C) and D))"...

You can always test how Perl parses snippets by using the Deparse
module --- from the command line:

~$ perl -MO=Deparse,-p -e '$a and $b || $c and $d'
(($a and ($b || $c)) and $d);
-e syntax OK


The -p option to the Deparse module tells it to add in parentheses,
without the -p it may show a logically equivelant form:

~$ perl -MO=Deparse -e '$a and $b || $c and $d'
$d if $a and $b || $c;
-e syntax OK

Hope that helps,
andrew
import-bot (20211) [Avatar] Offline
#3
Re: "or die"...
[Originally posted by lsanger]

That helps a lot! I wondered why sometimes I had to use parentheses after
'open' and sometimes not... Thanks!

--Larry