Maybe someone really meant to say that
my $hash = { a => 'b' }; $hash->{$x}
is just a lot more legible, intelligible, and therefor more maintainable than
{a => 'b'}->{$x}
and in terms of programmer time (as opposed to execution time), the former could be a time saver overall. That's probably a matter of personal judgment and taste, but I'd be inclined to agree with it. Especially as you get into larger numbers of hash elements, IMO, keeping the initialization and the fetching of a value as separate statements seems easier and more intuitive, somehow.

Still, now that I've seen the latter idiom (no, I hadn't seen that sort of usage before), I can appreciate its attraction -- it's sort of like a clever substitute for the (chain of) ternary operator(s). Instead of this:

sub some_function { my ( $x ) = @_; return ( $x eq 'foo' ) ? "results for foo" : ( $x eq 'bar' ) ? "results for bar" : ( $x eq 'qaz' ) ? "results for qaz" : " ... and so on, ad nauseum"; }
there's this:
sub some_function { my ( $x ) = @_; { foo => "results for foo", bar => "results for bar", qaz => "results for qaz", }->{$x} || "... and so on, ad nauseum"; }
It might be interesting to benchmark those alternatives, since they seem more "equivalent" (in terms of purpose) than the two you tested.

(updated to fix a typo, and to add the following benchmark script:)

#!/usr/bin/perl use strict; use Benchmark qw(:all); sub anonhash { my ( $x ) = @_; { foo => "results for foo", bar => "results for bar", qaz => "results for qaz", }->{$x} || "... and so on, ad nauseum"; } sub ternary { my ( $x ) = @_; return ( $x eq 'foo' ) ? "results for foo" : ( $x eq 'bar' ) ? "results for bar" : ( $x eq 'qaz' ) ? "results for qaz" : " ... and so on, ad nauseum"; } my @strings = qw/foo bar baz qaz/; my $i = 0; cmpthese( -5, { anonhash => sub { anonhash( $strings[$i++] ); $i = 0 if ( $i == @strings ) }, ternary => sub { ternary( $strings[$i++] ); $i = 0 if ( $i == @strings ) }, } );
and the results:
Rate anonhash ternary anonhash 64869/s -- -72% ternary 232475/s 258% --

Well, so much for being clever, I guess. ;)

One last update: I was still curious about "scalability": what happens as more distinct cases are needed? I adjusted my benchmark script by adding seven more hash keys to the anonhash function, and an equivalent set of seven more chained conditionals in the ternary function. Results:

Rate anonhash ternary anonhash 26893/s -- -87% ternary 204817/s 662% --

In reply to Re: How to access a static hash. by graff
in thread How to access a static hash. by gam3

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.