Hello
In the node for loops and 'and' several questions were raised about list context propagation. Much of the weirdness there is due to the semantics of the range (..) operator in scalar context.
My question is slightly more general. perlop says:
Scalar or list context propagates down to the right operand if it is evaluated.But from the following code, that doesn't seem to be the case. It seems that the left side is always getting scalar context, while right side always gets list. I know that for evaluation the lhs must be "booleanated," but then shouldn't the right side be too? Is it a question of DWIM going haywire? Thanks.
use strict; use warnings; sub leftt { wantarray ? print "Left is list\n" : print "Left is scalar\n"; return 1; } sub rightt { wantarray ? print "Right is list\n" : print "Right is scalar\n"; return 1; } sub leftf { wantarray ? print "Left is list\n" : print "Left is scalar\n"; return 0; } sub rightf { wantarray ? print "Right is list\n" : print "Right is scalar\n"; return 0; } print "true or true \n"; my @test = (leftt() or rightt()); print "\@test = @test\n"; print "true or false\n"; @test = (leftt() or rightf()); print "\@test = @test\n"; print "false or true\n"; @test = (leftf() or rightt()); print "\@test = @test\n"; print "false or false\n"; @test = (leftf() or rightf()); print "\@test = @test\n"; __DATA__ Output: true or true Left is scalar @test = 1 true or false Left is scalar @test = 1 false or true Left is scalar Right is list @test = 1 false or false Left is scalar Right is list @test = 0
In reply to Short circut operators in list context by jweed
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |