in reply to Empty List miracle (2)

Without having investigated it in more detail, I suspect that (empty_list)[0] is (), which turns into undef in scalar context, but is the empty list in list context. That's why you get 3 items passed to use_hash.

(In Perl 6 we call that value "Nil").

Replies are listed 'Best First'.
Re^2: Empty List miracle (2)
by JavaFan (Canon) on Apr 29, 2010 at 10:07 UTC
    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.

      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.