spacer
Web Development Tutorials CGI-PERL Tutorials
 Developer Newsletter

Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous


Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Developer Manuals
Learn HTML
Learn PHP
Learn CSS
Learn AJAX
Learn JavaScript
Learn Pear
Free White Papers

Developer Resources
Developer Tools
Developer Content
Survey Software
Dedicated Servers




Perl binding, or vs. ||

By Tony Lawrence
2005-06-16


Perl binding

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:

#!/usr/bin/perl

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.



Tutorial Pages:
» Perl binding


© Copyright 2005 A.P. Lawrence


 | Bookmark Print |   Write For Us
Related Tutorials:
» Random subroutines in Perl
» Log Script Use
» Creating Perl Modules for Web Sites
» Bit Vector, Using Perl Vec
» Build a Perl/CGI Voting System
» Perl Range Operator



About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: