Michael Fowler [mailto:[EMAIL PROTECTED]] wrote:
>The original discussion was regarding the alternatives:
>
> open(FILE, $file) || die("open: $!");
>
> open FILE, $file or die "open: $!";
>
>
>This is the matter of style of style being discussed. Naturally, if you're
>not using parens, the choice of 'or' or '||' becomes a little more
>important.
Granted. But, this is a beginner's list, and beginners will often do such
things as assuming "Well, leaving out the parens is okay, and exchanging
'or' for '||' is okay, so doing both must be okay as well! I know that I've
made a number of such blunders.
For a novice, I would probably recommend the alternative:
open (FILE, $file) or die ("open: $!");
but that's just me
>Your basic premise, that there's a pretty good reason to use 'or' instead
of
>'||', tries to generalize the problem too much. There are instances where
>it's much preferable to use '||', for the very reason you mention
preferring
>'or', precedence. Consider:
>
> $foo = undef || "default";
> $foo = undef or "default";
Again, granted. I was referring to the common perlism of "do something or
die" (substitute gripe,carp,custom routine, whatever for die at the end if
you so wish). My main point was that || and or are NOT synonymous in that
instance, which makes the usage of one or the other slightly more than a
matter of style, and wanted to point that out to others who may not have
been aware of the difference.
Perhaps I was not specific enough in my suggestion as to using 'or' instead
of '||'. I apologize for that.
>The alternative to using '||', while still accomplishing what was intended,
>is, of course:
>
> $foo = (undef or "default");
>
>I think you can guess which I prefer.
I'd probably prefer the form 'undef || "default"' as well. Though I'd
really probably prefer '(undef || "default")' just to make sure that perl
does exactly what I want, and because I do too much C/C++. Really, for a
beginner, I'd recommend putting most stuff in ()s when applicable, just
because that's a good way to avoid gotchas exactly like we've just
discussed.
When teaching someone a foreign language, you don't start with slang and
contractions.
I would still recommend, for a beginner, using 'or' in the "do something or
die" construct, just because it's less likely, in that particular construct,
to do something you don't expect.
Rob