in reply to Kindly let me know what exactly && does in Perl

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.

Replies are listed 'Best First'.
Re^2: Kindly let me know what exactly && does in Perl
by Anonymous Monk on Sep 07, 2014 at 15:10 UTC
    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";