in reply to An empty list in boolean context?
In Perl, () is a grouping construct. It does not create anything, it simply disambiguates things for the parser. Further, a list only exists in list context. There can not, by definition, be a list in scalar context. It would be nice if there was some more canonical documentation, but the best explanation I found is in perlfaq4, under the question "What is the difference between a list and an array?":
As a side note, there's no such thing as a list in scalar context. When you say
$scalar = (2, 5, 7, 9);you're using the comma operator in scalar context, so it uses the scalar comma operator. There never was a list there at all! This causes the last value to be returned: 9.
Update: digging further, I found the following in the "Truth and Falsehood" section of perlsyn:
The number 0, the strings '0' and '', the empty list "()", and "undef" are all false in a boolean context.
I wonder if () is a special case that always creates an empty list (if this is the case, then the earlier quoted section must be wrong in at least one case), or if this is just one of those simplifying lies that makes life so much easier?
Another update: just for the record, I'm of the mind that the "Truth and Falsehood" quote is a one of those lies that we use to make explaining something much easier. I'm leaning towards thinking of the () here as an empty statement -- and not a list at all -- which evaluates to false.
|
|---|