Like dws, I'm surprised this would run; it doesn't for me under 5.6.1. Your declaration of %p almost has the syntax for a hash slice, except that you can't do that in a my declaration. Something like my %p; @p{ 'eyes', 'hair', 'etc' } = (); would be legitimate, if unusual.

As for why do_head doesn't do what you think, I believe you're misunderstanding the behavior of undef in an array. undef in an array or hash doesn't just disappear, it's still something. This is notably different from its behavior in a list (which is not an array!). For instance, see the behavior of this snippet:

@a = ( ); @c = ( undef ); die "a" if @a; die "b" if ( undef, undef ); die "c" if @c

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 undefs give @data a true value. So, your list of undefs 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., die "d" if ( 1, undef ); die "e" if ( undef, 1 ); dies with 'e'). The solution, if you wish to keep the behavior of printing when the elements are defined, can be found in dws' post.


In reply to Re: Testing array of hash values by athomason
in thread Testing array of hash values by legLess

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.