APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

2005/06/08 Perl binding, "or" vs. "||"

© June 2005 Tony Lawrence
June 2005

Perl has "||" and "or". While "or" can't be used in bit operations, either one can be used in logical flow control - but there is an important difference between them. For example, this code doesn't work properly:

open FILE, "$file" ||  die "Can't open: $! \n";
print "$file open";

If "$file" doesn't exist, you won't get the "Can't open" message. The problem is that the "||" binds tightly and confuses the "open" function. You need to either do:

 open FILE, "$file" or die "Can't open: $! \n";

(because or binds less tightly than ||) or

 open(FILE, "$file") || die "Can't open: $! \n";

(because the parens contain the open function)

Generally speaking it's a very good idea for new Perl folk to use "or" rather than "||" in conditional flow tests, and to use parens for functions. So (following both rules), I'd recommend getting used to writing that like this:

 open(FILE, "$file") or die "Can't open: $! \n";

at least until you are very clear on what each of these does.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Perl binding, 'or' vs. '||'

Inexpensive and informative Apple related e-books:

Take Control of the Mac Command Line with Terminal, Second Edition

El Capitan: A Take Control Crash Course

Take Control of IOS 11

Photos: A Take Control Crash Course

Take Control of High Sierra

More Articles by © Tony Lawrence

Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

Today the theory of evolution is about as much open to doubt as the theory that the earth goes round the sun. (Richard Dawkins)

Linux posts

Troubleshooting posts

This post tagged:



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode