in reply to Re: Map Function Weirdness
in thread Map Function Weirdness

"That means that if evaluates to the same thing as its condition if the condition evaluates to something false, and if evaluates to the same thing as its body otherwise."

For what it's worth, B::Deparse has this to say about it:

perl -MO=Deparse,-x9 -e 'if($a<$b){$a;}' $a < $b and do { $a };

Looking at that makes a lot of sense: and being a logical short circuit means that if $a < $b evaluates to false, the right hand side is never evaluated. Consequently, the return value has to be the value of the relational expression.

On the other hand, if $a is less than $b, the conditional is true, and the logical and allows the right hand side to be evaluated.

if( $a < $b ) { $a } $a < $b and do { $a };

Two ways of looking at it. The latter seems to me to make a lot of sense.


Dave

Replies are listed 'Best First'.
Re^3: Map Function Weirdness
by ikegami (Patriarch) on Nov 09, 2011 at 17:56 UTC

    Keep in mind that Deparse is not precise. It's a useful tool, but don't rely on it too deeply. For example, this is wrong.

    >perl -MO=Deparse,-x9 -e"if(my $x=$a<$b){$a;}" my $x = $a < $b and do { $a }; -e syntax OK

    The $x is scoped to the if. Or is it...

    My code is an approximation too. The if is equivalent to neither

    do { ( $a < $b ) and do { $a } };

    nor

    ( $a < $b ) and do { $a };

    The my is lexically scoped at compile-time (as shown in mine), but it's not lexically scoped at run-time (as shown by Concise and Deparse). This means the objects can live a little longer than expected.

    ... { ... if (my $obj = ...) { ... } . . # $obj still exists here "anonymously". . } # $obj cleared here, so destructor only called here. ...