in reply to Re: Empty List miracle (2)
in thread Empty List miracle (2)

Without having investigated it in more detail, I suspect that (empty_list)[0] is ()
It is, in list context. In list context, such a construct is called a slice. Quoting perldata:
A slice of an empty list is still an empty list.
which turns into undef in scalar context
Perl doesn't work that way. It doesn't say, "let's evaluate EXPR in list context, then do a list-to-scalar conversion of the result". In scalar context, (empty_list)[0] is just the indexing operator. [0] is out of bounds (there's no first element), and hence, the result is undef.

Replies are listed 'Best First'.
Re^3: Empty List miracle (2)
by ikegami (Patriarch) on Apr 29, 2010 at 15:21 UTC

    In scalar context, (empty_list)[0] is just the indexing operator.

    It's still a slice. For example, you can do

    $x = ( f() )[2,3,4];

    The rest is accurate, though. In scalar context, the slice operator will always return exactly one element. It will never return an empty list that magically becomes a scalar.