Gossamer Forum
Home : General : Perl Programming :

'||' or 'or'

Quote Reply
'||' or 'or'
Hi

I can't remember where I read it, but I have read somewhere that you should use 'or' over '||' in some cases because || has a higher precedence than or and might give unexpected results? Anyone else know of anything like this?

I remember reading that when doing functions like

open FILEHANDLE
|| die ...

You should instead use

open FILEHANDLE
or die

Because || has a great presence than open. Does this make sense?

- wil
Quote Reply
Re: [Wil] '||' or 'or' In reply to
I don't know poop, but I do know that || is Perl 4 and 'or' was introduced in Perl 5. I'd like to know more about what you find out. Smile


Quote Reply
Re: [Watts] '||' or 'or' In reply to
OK. I remember I read this in Programming Perl, 3rd edition. I'll find the reference for you and post.

Cheers

- wil
Quote Reply
Re: [Wil] '||' or 'or' In reply to
You should use 'or' for things like:

open HANDLE, "foo" or die $!;

and || for stuff like:

my $var = 'something' || 'somethingelse';
Quote Reply
Re: [PaulW] '||' or 'or' In reply to
Yes, but did you see anything about different levels? || has a higher presencde than or which I found interesting.

- wil
Quote Reply
Re: [Wil] '||' or 'or' In reply to
Hi,

I'm sure the example was from programming perl book:

open FH, $file || die "oops: $!";

translates to:

open FH, ( $file || die "oops: $!" );

so basically it won't die if the file does not exist, or can't be opened.

or has a lower precedense so:

open FH, $file or die "oops: $!";

translates to:

open (FH, $file) or die "oops: $!";

which is what you want.

Cheers,

Alex


--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] '||' or 'or' In reply to
Bingo! Thanks for the explanation.

- wil
Quote Reply
Spelling of "precedence" In reply to
Hi guys,

I wouldn't have thought it possible that this innocent word can have so many different spellings. I am even more surprised that these different spellings were all used by native speakers....

Wil: precedence, i.e. correct first time, congratulations, but then presence ...
Wil again: presencde, seems to be slightly confused.
Alex: explains perl-well, but ... precedense


Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Spelling of "precedence" In reply to
:-)

- wil
Quote Reply
Re: [Wil] '||' or 'or' In reply to
'or' has a lower precedence than || - so low, in fact, that it is below Perl's binding of variables to function call arguments, assignments, ',' and '=>', etc. In fact, there is nothing lower than or/xor on the precedence table ('and' is just above, 'not' just above 'and').

When you run this code:

Code:
foo $var or die "Can't foo \$var";

perl will split it up on the or's:

Code:
foo $var
or
die "Can't foo \$var";

The variable '$var' will be passed as the only argument to foo.

On the other hand, when assigning:

Code:
$var = $other_var || 'abc';

the precedence of '=' is lower than ||, so the || gets evaluated first. The same with function calls:

Code:
foo $var || 'abc'; # Either going to call foo($var) or foo('abc')

If you get into the habit of always using (brackets) around every combination, it makes absolutely no difference which one you use. These are identical:

Code:
foo($var) or die;
foo($var) || die;

And these are the same too:

Code:
($a or $b || $c or $d)
($a || ($b or $c) || $d)

So, really, it isn't any "better" or "worse" to use '||' versus 'or' - so long as you know that 'or' binds very loosely. I just happen to think 'or' looks better - and it saves on ugly () in the code. Cool

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com