Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-23-2007, 05:23 AM   #16
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244

you can set up a small test: eg
$var = open(FILE,"<file") or print "cannot open";
print "\$var is  " . $var . "\n" ;
$var2 = open(FILE,"<file") || print  "cannot open";
print "\$var2 is " . $var2 . "\n";
where "file" doesn't exist...
here's my output:
# ./
cannot open$var is
cannot open$var2 is 1
Old 05-23-2007, 06:03 AM   #17
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,412

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
From Perl docs ( )

"Logical or and Exclusive Or

Binary "or" returns the logical disjunction of the two surrounding expressions. It's equivalent to || except for the very low precedence. This makes it useful for control flow

print FH $data or die "Can't write to FH: $!";

This means that it short-circuits: i.e., the right expression is evaluated only if the left expression is false. Due to its precedence, you should probably avoid using this for assignment, only for control flow.

$a = $b or $c; # bug: this is wrong
($a = $b) or $c; # really means this
$a = $b || $c; # better written this way

However, when it's a list-context assignment and you're trying to use "||" for control flow, you probably need "or" so that the assignment takes higher precedence.

@info = stat($file) || die; # oops, scalar sense of stat!
@info = stat($file) or die; # better, now @info gets its due

Then again, you could always use parentheses. "

See last 2 examples here.

Edit: re ghostdog's example above, I always use something like
... or print "Cannot open file <filename>: $!\n";
where $! is perl's built-in error code marker/special var (auto translates to error msg)

Last edited by chrism01; 05-23-2007 at 06:07 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
ftp listing does not work Lotharster Linux - Newbie 2 11-28-2005 08:42 AM
Recursively listing files over FTP? overbored Linux - Software 4 10-14-2004 01:12 PM
Getting Apache to spawn FTP listing? Mamour Linux - Networking 1 09-02-2003 04:52 PM
how can I restrict ftp users listing files from a pure-ftp server adrianmak Linux - Networking 2 12-31-2002 09:23 AM
Ftp listing problem piranha Linux - Networking 1 01-21-2002 01:35 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:04 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration