Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

dereference hash

by fionbarr (Friar)
on Jul 09, 2015 at 16:34 UTC ( [id://1133985]=perlquestion: print w/replies, xml ) Need Help??

fionbarr has asked for the wisdom of the Perl Monks concerning the following question:

I generally code like:
print $$hash_ref{$key};
is there a value in:
print $hash_ref->{$key}

Replies are listed 'Best First'.
Re: dereference hash
by Athanasius (Archbishop) on Jul 09, 2015 at 17:13 UTC

    Hello fionbarr,

    Suppose you were silly unfortunate enough to have code like this:

    2:56 >perl -wE "my %h = ( A => 'B' ); my $h = { A => 'C' }; say $h{A} +; say $$h{A}; say $h->{A};" B C C 3:15 >

    That is, two (separate, unrelated) hashes both called h: a hash named %h, and an unnamed hash accessible via the scalar reference $h. $h{A} and $$h{A} would then be quite different things, but they would look so similar that it would be only too easy to confuse them. In a case like this, the value of the dereference syntax $h->{A} becomes apparent: it makes the dereference operation visually distinctive, and so reduces the danger of confusion.

    Anyway, those of use who came to Perl from C++ find the arrow dereference syntax comfortingly familiar; it just “looks right.” ;-)

    But, as always, TMTOWTDI, and YMMV.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      informative reply: thanks
Re: dereference hash
by BrowserUk (Patriarch) on Jul 09, 2015 at 17:24 UTC

    Whenever I see code like: print $$hash_ref{$key}; I have to think hard as to whether that is equivalent to:

    print ${ $hash_ref }{ $key };

    Or:

    print ${ $hash_ref{ $key } };

    With the second form, I don't have that indecision.

    In a similar vein, given this sub:

    sub x{ print $$_[0][1] };;

    What do you think these two calls print?:

    x( [['a','b'],['c','d']] );; x( ['a','b'],['c','d'] );;

    Produces:

    Is it clearer if the sub is defined this way?:

    sub x{ print $_[0]->[1] };; xx( [['a','b'],['c','d']] );; xx( ['a','b'],['c','d'] );;

    Produces:


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
    I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
Re: dereference hash
by Anonymous Monk on Jul 09, 2015 at 20:57 UTC
Re: dereference hash
by stevieb (Canon) on Jul 10, 2015 at 19:18 UTC

    Great responses already...

    I use what is easier to spot when I'm glancing at code. This:

    $hash->{thing};

    is easier to see at a glance than:

    $$hash{thing};

    Likewise, I find:

    %{$hash};

    easier to understand in a quick pass than:

    %$hash

    -stevieb

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1133985]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-26 08:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found