pritesh has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am trying out the following script in Perl

#!/usr/bin/perl use warnings; use strict; my $zero = 0; my $two = 2; my $three = 3; my $x = $zero && $two; my $y = $three && $zero; my $z = $two && $three; print "\$x is = $x\n"; print "\$y is = $y\n"; print "\$z is = $z\n";

And here is what it prints

$x is = 0 $y is = 0 $z is = 3

I thought that the && would sort of "short circuit" and I was expecting:

$x is = 0 $y is = 3 $z is = 2

But that's not happening. I'm on Stella Linux (CentOS Remix) Version 6.5 (64 Bit) and Perl Version 5.10.1

I am darn sure I have messed up my understanding. Can someone please help explain this?

Replies are listed 'Best First'.
Re: Kindly let me know what exactly && does in Perl
by BrowserUk (Patriarch) on Sep 07, 2014 at 14:53 UTC
    I thought that the && would sort of "short circuit"

    It does, but it is a logical operator hence this:

    ## The left side is false, so it short circuits ## and the residual value assigned is 0 my $x = $zero && $two; ## The left side is true, so it checks the right side which is false; ## so the residual value comes from the right side and is 0 my $y = $three && $zero; ## The left side is true, so it checks the right side which is also tr +ue; ## so the residual value comes from the right side and is 3 my $z = $two && $three;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Kindly let me know what exactly && does in Perl
by ikegami (Patriarch) on Sep 07, 2014 at 15:03 UTC

    Short-circuiting doesn't mean it never evaluates the right operand; short-circuiting means it only evaluates as much as possible to determine the answer. It will still always give the right answer. The truth table for AND is:

    false && false => false \ Short circuit possible false && true => false / true && false => false true && true => true

    If the LHS is false, it will never evaluate the RHS because of short-circuiting. If the LHS is true, it must evaluate the RHS.

    But more importantly, the answer you expect is wrong. True and false shouldn't give true.


    Here's a demonstration of short-circuiting in effect.

    sub zero { print "zero\n"; 0 } sub two { print "two\n"; 2 } sub three { print "three\n"; 3 } my $x = zero && two; print "\$x is = $x\n\n"; my $y = three && zero; print "\$y is = $y\n\n"; my $z = two && three; print "\$z is = $z\n\n";
    zero $x is = 0 <--- two never called three zero $y is = 0 two three $z is = 3
Re: Kindly let me know what exactly && does in Perl
by Anonymous Monk on Sep 07, 2014 at 14:43 UTC

    See perlop: The ||, // and && operators return the last value evaluated (unlike C's || and &&, which return 0 or 1).

    For $y and $z, the first operand is true, so && needs to look at the second operand to determine if both are true. The && for $x can short-circuit after seeing that the first operand is false, thus knowing the AND expression will be false.

    As for your expected value of $y, remember that 0 is false, so you shouldn't expect the result of the operation to be a true value.

    You're probably thinking of ||, which can short-circuit if the first operand is true.

      You're probably thinking of ||

      If you replace && with || in the code, || would short-circuit on $y and $z, but not on $x.

      Short-circuiting basically means to stop evaluating a logical expression once its outcome is known. For logical And, that means stop evaluating once the first false value is seen, for logical Or, stop once the first true value is seen.

      Short-circuit behavior, especially Perl's behavior of returning the last evaluated value, can be useful for:

      1. efficiency, e.g. if (complex_operation() && other_complex_operation()) { ... } doesn't call other_complex_operation() if complex_operation() is false
      2. combining statements, e.g. $_%3 && print "$_%3 is true" for 0..10 (though readability can suffer that way), or open(...) or die $!;
      3. setting a variable to one value or another, e.g. my $option = $user_option||"default value";
Re: Kindly let me know what exactly && does in Perl
by LanX (Saint) on Sep 07, 2014 at 16:53 UTC
    You got many detailed answers lets try a short one. :)

    The first decisive value is returned by || and &&.

    If the whole && expression is true can only be known by the last value (which must also be true and is returned), otherwise the first false value is returned.

    And vice versa for || - just swap true and false.

    HTH :)

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    corrections

    Updated wording, see reply for details.

      If the whole && expression is true can only be known with the last true value, ...

      That wording leaves open the possibility that there might be more false values after the "last true value"... perhaps better:

      The whole && expression is only true if all its operands are true, in that case the last true value is returned; otherwise the first false value is returned.

        True!

        or just:

        The first false value (if existing) is returned, otherwise the last one (which is hence true).

        Hope I can transport the idea, feel free to correct my English! :)

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: Kindly let me know what exactly && does in Perl
by pritesh (Scribe) on Sep 08, 2014 at 05:54 UTC

    Hi LanX,

    Thank you for taking time to answer the question. Thanks once again to the Anonymous Monks who are providing tips. No one here berated me stating "This is a stupid question", and have given excellent clarifications. Thank you Monks. I am not able to upvote all the answers because I have just two votes and I used them for upvoting few of the answers here today.

    I am returning to Perl after a brief tryst. A friend of mine gave me a copy of Beginning Perl by Ovid Poe and stated that it should be good enough to get me started. I'm glad I did. Its a great book. And Perlmonks is a great place to get answers for questions that might arise.

    Regards,

    Pritesh

      Your welcome, and it's not a stupid question.

      But please note: this is not a flat board where every reply has to be appended at the thread's end, you can comment on every individual post of this discussion tree directly. :)

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

        Hi LanX

        Noted. :)

Re: Kindly let me know what exactly && does in Perl
by pritesh (Scribe) on Sep 07, 2014 at 15:19 UTC

    Hi,

    Thank you anonymous monk and browseruk.

Re: Kindly let me know what exactly && does in Perl
by pritesh (Scribe) on Sep 07, 2014 at 16:00 UTC

    Hi Ikegami,

    Thank you for taking time to anwer the question.