in reply to Empty List miracle(1)
the function is called in scalar context, so that the number of list elements should be printed.
That's not what scalar context does. Scalar context simply lets operators know they can only return a single scalar value. What that value is up to each individual operator.
Some operators (e.g. @array, grep) return the number of values that would otherwise be returned, but that's far from typical. The most common practice is to return exactly the same thing in every context.
Looking at your code, you have a number of subs evaluated in scalar context. That means their return expression is evaluated in scalar context. Your code is equivalent to
print(scalar( @{ [] } ),"\n"); print(scalar( qw,, ),"\n"); print(scalar( () ),"\n");
which the tokenizer changes into
print(scalar( @{ [] } ),"\n"); print(scalar( () ),"\n"); print(scalar( () ),"\n");
In the first case, you have an array lookup in scalar context. That returns the number of elements in the array being looked up, which is zero.
In the second and third case, you have a list literal in scalar context. That returns the last element of the list after evaluating it scalar context. If the list is empty, undef is returned.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Empty List miracle(1)
by rovf (Priest) on May 03, 2010 at 12:58 UTC |