- or download this
---------------------------------------------------------
$this && $that | If $this is true, return $that,
...
$this || $that | If $this is true, return $this,
$this or $that | else return $that.
---------------------------------------------------------
- or download this
my ($first, $second) = ( 1, 1 );
print "Truth\n" if $first++ && $second++;
...
Truth
First: 2
Second: 2
- or download this
my ($first, $second) = ( 1, 0 );
print "Truth\n" if $first++ && $second++;
...
# Note: Both are evaluated because the first one is
# true. But "Truth" isn't printed, because only one
# of the two expressions evaluated "true".
- or download this
my ($first, $second) = ( 0, 1 );
print "Truth\n" if $first++ && $second++;
...
Second: 1
# $second didn't get incremented because the
# evaluation stopped when $first evaluated false.
- or download this
my ($first, $second) = ( 0, 0 );
print "Truth\n" if $first++ && $second++;
...
Second: 0
# $first was evaluated for truth. It was false,
# so $second didn't get evaluated.
- or download this
my ($first, $second) = ( 1, 1 );
print "Truth\n" if $first++ || $second++;
...
Second: 1
# Since $first is true, no need to evaluate $second;
# we already know that the 'or' expression is true.
- or download this
my ($first, $second) = ( 1, 0 );
print "Truth\n" if $first++ || $second++;
...
Second: 0
# Again, $second is never evaluated because $first
# is true, and that's good enough for ||.
- or download this
my ($first, $second) = ( 0, 1 );
print "Truth\n" if $first++ || $second++;
...
# Both sides got evaluated because since $first was
# false it was necessary to evaluate $second to
# determine if truth exists (it does).
- or download this
my ($first, $second) = ( 0, 0 );
print "Truth\n" if $first++ || $second++;
...
Second: 1
# There is no truth. Both expressions were
# evaluated to find it.
- or download this
open( FILE, "<filename" ) || die "Cannot open filename: $!\n";
# Open the file. If the open fails (and thus evaluates
# false) fall through to the second half: die.
- or download this
local $_ = 'xyz';
SWITCH: {
...
/^xyz/ && do { $xyz = 1; last SWITCH; };
$default = 1;
}
- or download this
if ( $this && $that && $other ) { print "Truth\n"; }
- or download this
my $client = $ENV{USER_HOST} ||
$ENV{USER_ADDR} ||
"UNKNOWN";
- or download this
my @sorted = sort { uc($a) cmp uc($b) || $a cmp $b } @unsorted;
- or download this
my @list = ( { 'Name' => "Pete", 'Age' => 32 },
{ 'Name' => "Pete", 'Age' => 55 } );
my @sorted = sort { $a->{'Name'} cmp $b->{'Name'} ||
$a->{'Age' } <=> $b->{'Age' } } @list;
- or download this
open ( FILE, "<filename" ) || die ..... # You already saw this one.
open FILE, "<filename" or die .... # Notice how the much lower
# precedence of "or" makes the parenthesis unnecessary in
# this case.