I think you are slightly confused about references. Taking the reference of a variable does not return its name (although this is a clever idea). Is this why you expect @attrs to contain qw(nam pwd uid ..)?

The name of a variable can be used as a "soft reference" (only when the referent is a symbol table variable and only when in no strict 'refs'). But think of it, not all scalar values even have names, and some have many! Because of this, taking a reference to any variable using the backslash operator creates a special "hard reference" scalar. When you print out something of this reference type, you get SCALAR(0x81717c4) (if the referent is a normal scalar). Perhaps consult perlreftut for more detailed info?

A better way to fill your hash the way you want is like this:

my @fields = qw/nam pwd uid gid dsc hom shl/; while (<F>) { my %attrs; @attrs{@fields} = split /:/, $_; $password->{ $attrs{nam} } = \%attrs; }
Again, you'll need an explicit list of keys for the hash, as you can't extract the attribute names from the variables.

blokhead


In reply to Re: SCALAR output instead of attribute name. by blokhead
in thread SCALAR output instead of attribute name. by sschneid

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.