Okay, I'll concede this one:
$ perl -wMstrict -le 'if((2,1,0)) {print"true"}else{print"false"}'
Useless use of a constant (2) in void context at -e line 1.
false
Although that's the comma operator operating in scalar context, (2,1,0) might be what people might think of when they think "list". But:
$ perl -wMstrict -le 'if(()=(2,1,0)) {print"true"}else{print"false"}'
true
although admittedly that's the result of a list assignment in scalar context.
In this thread we're talking about return, and I haven't yet found a way for a return 0; in some kind of list context to be evaluated as false:
$ perl -wMstrict -le 'sub foo { print wantarray?"list":"scalar";
return (0); }; if(foo) {print "true"} else {print "false"} '
scalar
false
$ perl -wMstrict -le 'sub foo { print wantarray?"list":"scalar";
return (0); }; if(()=foo) {print "true"} else {print "false"} '
list
true
$ perl -wMstrict -le 'sub foo { print wantarray?"list":"scalar";
return (0); }; my @x=foo;if(@x) {print "true"} else {print "false"}'
list
true
Although those last two are respectively, a list assignment and an array, not exactly a list.
So in the above examples, it's never really the lists which are directly in boolean context, so I kind of get what you are saying. But then again, the way I see it conceptually, in Perl, when you evaluate lists/arrays in a scalar context they evaluate to one of two things: their last element (although that one is usually the result of the comma operator in scalar context), or a count of their elements - and at least in my mind that's the underlying principle why a list with >0 elements is always "true". I haven't yet been able to come up with a counterexample where a list with at least one element somehow fiddled into a boolean context evaluates to false; if you've got one, I don't mind being proven wrong.
For my final argument, perlsyn, emphasis mine:
The number 0, the strings '0' and "", the empty list (), and undef are all false in a boolean context. All other values are true.
|