Subtle things in perl part 2: The difference between ‘or’ and ||

In perl, both ‘or’ and || mean the same thing. But they act in different ways when used in certain circumstances. For example, lets say we are trying to open a file for read and write operations. It’s always a good idea to handle the IO errors and we do this in the following way:

………………..
$file = shift;
open FILE, $file or die “Couldn’t open the file $file”; #note this
…………………

The second step can also be written in this way:
open FILE, $file || die “Couldn’t open the file $file”; #note this

But when the execution of this program happens, you will never see the message in ‘die’, if you use a || instead of ‘or’. Reason for this is that the || operator has a precedence level higher than that of ‘or’ and since
the ‘open FILE, $file’ returns a value after an attempt to execute, the || is always true and die is never reached
.

The actual reason as Martin, one of the commentators  puts it,

The actual reason is that the operator precedence for the double pipe is higher than the comma, so perl is resolving the double pipe first, which chooses the first true result of its two parameters, in this case $file and the die statement. As long as $file is a nonempty string it counts as true and always gets returned by the double pipe.

So it is always advisable to use ‘or’ instead of ||. If you want to use ‘||’, you can do it by using the
pen file with parathesis which ensures the precedence of the operators.

open (FILE, $file) || die “Couldn’t open the file $file”;

Advertisements

2 Responses to “Subtle things in perl part 2: The difference between ‘or’ and ||”

  1. Your advice is good but your reason is a bit off.

    “Reason for this is that the || operator has a precedence level higher than that of ‘or’ and since the ‘open FILE, $file’ returns a value after an attempt to execute, the || is always true and die is never reached.”

    No, open returns false if it fails. It’s just not running at all. The actual reason is that the operator precedence for the double pipe is higher than the comma, so perl is resolving the double pipe first, which chooses the first true result of its two parameters, in this case $file and the die statement. As long as $file is a nonempty string it counts as true and always gets returned by the double pipe.

    To prove it try saying this with an empty $file variable: open FILE, $file || ‘/etc/aliases’;

  2. vinaychilakamarri Says:

    Wow that turned out to be subtler than what I could understand. Thanks martin for the correction. I’ll include it in the post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: