Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Tutorial RFC: Guide to Perl references, Part 1

by djrosenblatt (Initiate)
on May 03, 2019 at 17:01 UTC ( [id://1233323]=note: print w/replies, xml ) Need Help??


in reply to Tutorial RFC: Guide to Perl references, Part 1

Thank you for this tutorial. You've helped clear up one thing that has probably caused me some confusion when reading others' Perl code, which is that the curly braces have two different purposes. One is to dereference arrays and hashes. For example:

my @myarray = @{ $aref } ; my %myhash = %{ $href } ;

The other use is to reference a hash value by means of its key:

my $hash_value = $myhash{ $keyA } ;

But is there some consistent logic to this? That is, does the internal Perl logic consider a key of a hash to essentially be a reference to the storage holding its associated value?

And are there any other "gotchas" in Perl that typically cause confusion in newbies?

Thanks!

Replies are listed 'Best First'.
Re^2: Tutorial RFC: Guide to Perl references, Part 1
by LanX (Saint) on May 03, 2019 at 17:25 UTC
    > curly braces have two different purposes. One is to dereference arrays and hashes. For example

    curly braces have more purposes

    • grouping blocks like if (1) { CODE }
    • literal anonymous hashes $href = { KEY => VALUE }
    • accessing hash keys $href->{KEY} or $hash{KEY}

    This my %myhash = %{ $href } ; is just a form of grouping like in the first example to clarify the precedence.

    Which doesn't make much sense here since my %myhash = %$href ; has the exact same effect.

    And it's not the curly which does the dereference, but the %sigil.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2024-04-23 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found