LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

Rep: Reputation: 241Reputation: 241Reputation: 241

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

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
From Perl docs ( http://perldoc.perl.org/perlop.html )

"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 05:07 AM.
 
  


Reply


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 07:42 AM
Recursively listing files over FTP? overbored Linux - Software 4 10-14-2004 12:12 PM
Getting Apache to spawn FTP listing? Mamour Linux - Networking 1 09-02-2003 03:52 PM
how can I restrict ftp users listing files from a pure-ftp server adrianmak Linux - Networking 2 12-31-2002 08:23 AM
Ftp listing problem piranha Linux - Networking 1 01-21-2002 12:35 AM


All times are GMT -5. The time now is 05:56 AM.

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