Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Maybe someone can explain || vs or

Status
Not open for further replies.

travs69

MIS
Dec 21, 2006
1,431
US
I always thought they were interchangeable till today.
Code:
	my @trapd_file_list = qx{$remsh $trapd_log_server $ls $trapd_log_path/$trapd_server} || &senderror("$remsh $trapd_log_server $ls $trapd_log_path/$trapd_server : $!", 'exit');
	print "$#trapd_file_list\n";
	my @trapd_file_list = qx{$remsh $trapd_log_server $ls $trapd_log_path/$trapd_server} or &senderror("$remsh $trapd_log_server $ls $trapd_log_path/$trapd_server : $!", 'exit');
	print "$#trapd_file_list\n";
	my @trapd_file_list = qx{$remsh $trapd_log_server $ls $trapd_log_path/$trapd_server};
	print "$#trapd_file_list\n";
which prints
0
744
744

I found very little explaining || vs or, I can't get to perldoc or cpan right now.. and only found a small tidbit about || binding more tightly on google, but didn't really have a good explanation.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
C-Style logical operators (|| , &&) will evaluate to 0 or 1. Perl's operators return the last value evaluated. This can be used to your advantage in an expression like
$a=$b||$c||$d;
which will assign to $a the value of the first scalar that is true (=different from zero)

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
general rule of thumb, stick to "or" for flow control, use || for assignment. "or" has the lowest precedence of all perl operators.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
rvBasic said:
C-Style logical operators (|| , &&) will evaluate to 0 or 1. Perl's operators return the last value evaluated.

That's not the case. From perlop:
The "||" and "&&" operators return the last value evaluated (unlike C's "||" and "&&", which return 0 or 1).
The difference between || and or is their precedence.
 
ishnid You're absolutely right: I wrongly added these symbols to the C-style operators. Leave them out and the eample is fine.

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top