in reply to Re: beginner syntax question
in thread beginner syntax question

Watch the precedence of your ors! Compare the following lines of code:

my $foo = $bar || die "Bela Lugosi is dead: $!"; my $FOO = $BAR or die "I'm dead, I'm dead, I'm dead";

s0ttle's code looks pretty good (but I haven't run it), and dragonchild's point about moving variable declarations to get the smallest scope possible is good too. But their use of  open() || die makes me nervous.

Remember that || has a very high precedence and or has a low precedence. In the code at the top of my post, line 1 dies if $bar evaluates to false; line 2 dies if the assignment to $FOO fails.

I've been burned by using the || die syntax before. I kept doing it after some kindly monk (I forget who) warned me not to, and doomed myself to spend much time debugging as a result.


Like Cassandra, TGI says moo

Replies are listed 'Best First'.
Re: or before , for comes before or
by buckaduck (Chaplain) on Sep 19, 2001 at 20:40 UTC
    While I understand your point and agree to a certain extent, I have to say that there is nothing wrong with using || in the following code:
    open(FILE, "filename") || die "Can't open filename: $!";
    There is more than one way to deal with operator precedence. One way is to substitute the or operator as you suggested. But a perfectly acceptable alternative is to use parentheses to make the precedence explicit. That's what was done is this case. It may make you nervous, but it's still perfectly acceptable (and in fact easier for novices to understand).

    buckaduck