note
athomason
Like [dws], I'm surprised this would run; it doesn't for me under 5.6.1. Your declaration of <code>%p</code> almost has the syntax for a hash slice, except that you can't do that in a <code>my</code> declaration. Something like <code>my %p; @p{ 'eyes', 'hair', 'etc' } = ();</code> would be legitimate, if unusual.<p>
As for why do_head doesn't do what you think, I believe you're misunderstanding the behavior of <code>undef</code> in an array. <code>undef</code> in an array or hash doesn't just disappear, it's still <i>something</i>. This is notably different from its behavior in a list (which is not an array!). For instance, see the behavior of this snippet:<p>
<code>@a = ( ); @c = ( undef ); die "a" if @a; die "b" if ( undef, undef ); die "c" if @c</code><p>
Particularly, it dies with 'c', not 'a' or 'b'. In a boolean context--like an if statement--an array takes the value of its length, not its members. In my example, this is 1, which is a true value. Likewise, your three <code>undef</code>s give @data a true value. So, your list of <code>undef</code>s is in fact true when assigned to an array. You were probably expecting the behavior of a list, which in a boolean context evaluates to its last element (e.g., <code>die "d" if ( 1, undef ); die "e" if ( undef, 1 );</code> dies with 'e'). The solution, if you wish to keep the behavior of printing when the elements are defined, can be found in [dws]' [id://130851|post].
130850
130850