Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Confused on Scalar/Hash

by csharp2a (Novice)
on Apr 02, 2018 at 15:30 UTC ( [id://1212170]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, I have encountered a use of scaler context that has me a bit confused. I would expect the following snippet to actually be a hash however the coder has used the scaler context. If this is correct, would someone be able to help me understand why it would not be a hash? Sorry for the elementary question, but it has caused confusion.

my $headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'charset' => 'UTF-8' };

Thank you.

Replies are listed 'Best First'.
Re: Confused on Scalar/Hash
by toolic (Bishop) on Apr 02, 2018 at 15:33 UTC
Re: Confused on Scalar/Hash
by stevieb (Canon) on Apr 02, 2018 at 15:34 UTC

    That's a scalar that contains a reference to a hash.

    Observe (untested):

    my %hash = ( a => 1, b => 2, ); my $href = \%hash;

    The coder just skipped the intermediary step of taking a reference to the hash, and instead constructed it as a hash reference directly. To get information from the hash as I depicted above, you access it normally:

    my $thing = $hash{a};

    In the case of the hash reference, you have to dereference first:

    my $thing = $href->{a};

    You can dereference the entire hash reference and put it into a new hash if you please, but there's not often reason to do so unless you need a copy of the entire structure:

    my %hash = %{ $href };

    See perlreftut.

Re: Confused on Scalar/Hash
by AnomalousMonk (Archbishop) on Apr 02, 2018 at 16:05 UTC
Re: Confused on Scalar/Hash
by csharp2a (Novice) on Apr 02, 2018 at 15:57 UTC
    Now I see it. It makes complete sense. I was not aware that you could use this method. Something new for my coding bag of tricks. Thank you all for the excellent explanations! Craig
Re: Confused on Scalar/Hash
by LanX (Saint) on Apr 02, 2018 at 15:41 UTC
    I personally understand %hash as the "list form" of a hash.

    You assign lists and you get lists of elements.

    i.e. %hash2 = %hash will copy the lists of key-value pairs into a new hash.

    $hash_ref = \%hash is a pointer to the container.

    $hash_ref2 = $hash_ref will share the same hash.

    changing $hash_ref2->{a}++ will also effect $hash_ref

    Many other languages only know the reference form. Like in JS hash={...} is a hash_ref.

    HTH!

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery

Re: Confused on Scalar/Hash
by Anonymous Monk on Apr 02, 2018 at 16:47 UTC
    You have no doubt heard the terms, "hashref" and "arrayref," being bantered-about here. Now you know.

      The kid you take off the bench to play for the last 90 seconds of the game when you're winning by 24 points.

      You have no doubt heard the terms, "hashref" and "arrayref,"...

      Actually, nope, nobody but yourself has mentioned "arrayref" in this entire thread ;)

      /pedantic

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-16 10:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found