Unless there is something that you've omitted by the simplification for the OP example code, there is no utility at all in building a named hash from a constant list via an anonymous hash.

In your modified example above, there is the potential for utility, though it is not realised. That of retaining a copy of the original (anonymous) hash. This only comes about because a reference to the anonymous hash is retained which it isn't in the OP.

This allows modifications to be made to the copy whilst retaining the original. This can be used for setting defaults in object constructors and the like:

package someclass; my $defaults = { foo=>1, bar=>23 }; sub new { my $class = shift; my %self = ( %{ $defaults }, @_ ); return bless \$self, $class; }

Here keys foo and bar in $self will get the default values unless they are overidden by named arguments on the constructor, and $defaults remains unmodified.

Though there is little merit in using a named anonymous hash rather than real hash unless $defaults is passed to a subroutine somewhere.

ps. I didn't say it didn't work. Only that there was no point to it! ;)


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^3: What's the difference between a hash ? by BrowserUk
in thread What's the difference between a hash ? by syphilis

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.