There's more than one way to do things PerlMonks

### Perl Idioms Explained - && and || "Short Circuit" operators

by davido (Archbishop)
 on Oct 22, 2003 at 20:28 UTC Need Help??

```---------------------------------------------------------
\$this && \$that   |    If \$this is true, return \$that,
...
\$this || \$that   |    If \$this is true, return \$this,
\$this or \$that   |    else return \$that.
---------------------------------------------------------
```
```my (\$first, \$second) = ( 1, 1 );
print "Truth\n" if \$first++ && \$second++;
...
Truth
First: 2
Second: 2
```
```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".
```
```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.
```
```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.
```
```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.
```
```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 ||.
```
```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).
```
```my (\$first, \$second) = ( 0, 0 );
print "Truth\n" if \$first++ || \$second++;
...
Second: 1
# There is no truth.  Both expressions were
# evaluated to find it.
```
```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.
```
```local \$_ = 'xyz';
SWITCH: {
...
/^xyz/ && do { \$xyz = 1; last SWITCH; };
\$default = 1;
}
```
```if ( \$this && \$that && \$other ) { print "Truth\n"; }
```
```my \$client = \$ENV{USER_HOST} ||
"UNKNOWN";
```
```my @sorted = sort { uc(\$a) cmp uc(\$b) || \$a cmp \$b } @unsorted;
```
```my @list = ( { 'Name' => "Pete", 'Age' => 32 },
{ 'Name' => "Pete", 'Age' => 55 } );
my @sorted = sort { \$a->{'Name'} cmp \$b->{'Name'} ||
\$a->{'Age' } <=> \$b->{'Age' }   } @list;
```
```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.
```

Create A New User
Node Status?
node history
Node Type: perlmeditation [id://301355]
Approved by ybiC
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2018-03-24 12:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (298 votes). Check out past polls.

Notices?