in reply to Re^4: map in void context
in thread map in void context

It's not really returning "true", it's returning the value on the right side of the assignment.

my $x = 2; print "\$x is '$x'\n"; if ( $x = 0 ) { print "\$x = 0 is true\n"; } else { print "\$x = 0 is false\n"; } print "\$x is '$x'\n"; __END__ $x is '2' $x = 0 is false $x is '0'

This behavior is why you can say "$x = $y = $foo" and set both $x and $y to whatever is in $foo.

If you consider the "effect" of assignment to be returning the value of the right hand side, then perhaps changing the value of the variable is a side effect. I don't see it that way, however.

Replies are listed 'Best First'.
Re^6: map in void context
by ysth (Canon) on Dec 22, 2008 at 23:03 UTC
Re^6: map in void context
by pat_mc (Pilgrim) on Dec 21, 2008 at 21:02 UTC
    Hi, kyle -

    Thanks for correcting my comment regarding the return value of the assignment operator =. I guess, the actual return value (namely the value assigned) is a perfect example for the informative return values of Perl operators I was commenting on earlier - and further down - in this thread.
    If you consider the "effect" of assignment to be returning the value of the right hand side, then perhaps changing the value of the variable is a side effect.
    I would say it exactly the opposite way: The effect of assignment is assigning a value. Its side-effect is that it also provides a return value (which just makes me wonder about list assignments ... must check if their return value really is a list of all assigned values).

    But OK - I think we now understand the way the other party is seeing things. Agreement must not necessarily be achieved. Thanks for responding again, anyway.

      which just makes me wonder about list assignments ... must check if their return value really is a list of all assigned values

      In list context, list assignment evaluates to the list of elements actually assigned.

      my @x = ($a,$b) = (4,5,6); # @x = (4,5)

      In scalar context, list assignment evaluates to the number of elements to assign.

      my $x = ($a,$b) = (4,5,6); # $x = 3

      Remember, x = y = z means x = ( y = z ) because assignment is right-associative.

        Thanks, ikegami, for the clear proof that indeed the side effect of list assignment is that a list of all values assigned is returned!

        This is a consistent extension of the behaviour of the assignment operator in scalar context and exactly in line with what I would have expected.

        To re-state my point: ikegami's code only works because of the side-effects associated with the assignment operator (To my personal surprise, ikegami mentioned earlier in this thread that
        Side-effects are something to be avoided when programming.