Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by davido (Archbishop)
on Oct 22, 2003 at 20:28 UTC ( #301355=perlmeditation: print w/ replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. 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.
    ---------------------------------------------------------
    
  2. or download this
    my ($first, $second) = ( 1, 1 );
    print "Truth\n" if $first++ && $second++;
    ...
    Truth
    First: 2
    Second: 2
    
  3. 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".
    
  4. 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.
    
  5. 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.
    
  6. 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.
    
  7. 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 ||.
    
  8. 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).
    
  9. 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.  
    
  10. 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.
    
  11. or download this
    local $_ = 'xyz';
    SWITCH: {
    ...
        /^xyz/ && do { $xyz = 1; last SWITCH; };
        $default = 1;
    }
    
  12. or download this
    if ( $this && $that && $other ) { print "Truth\n"; }
    
  13. or download this
    my $client = $ENV{USER_HOST} ||
                 $ENV{USER_ADDR} ||
                 "UNKNOWN";
    
  14. or download this
    my @sorted = sort { uc($a) cmp uc($b) || $a cmp $b } @unsorted;
    
  15. 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;
    
  16. 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.
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://301355]
Approved by ybiC
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (15)
As of 2015-07-28 13:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (254 votes), past polls