Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

meaning of '@' sigil on a hash?

by awohld (Hermit)
on Dec 08, 2007 at 17:42 UTC ( #655850=perlquestion: print w/ replies, xml ) Need Help??
awohld has asked for the wisdom of the Perl Monks concerning the following question:

I have this code and wonder why it works this way with a '@' preceding the hash:
#!/usr/bin/perl -w use strict; use Data::Dumper; my %hash; my @keys = qw ( one two three ); my @values = qw ( 1 2 3 ); # What's with the @ symbol and not a $? @hash{@keys} = @values; print Dumper \%hash;
I'd expect it to be as below:
#!/usr/bin/perl -w use strict; use Data::Dumper; my %hash; my @keys = qw ( one two three ); my @values = qw ( 1 2 3 ); # I thought it would need a $! $hash{@keys} = @values; print Dumper \%hash;
Why do you need the '@' preceding the hash name and not a '$'?

Comment on meaning of '@' sigil on a hash?
Select or Download Code
Re: meaning of '@' sigil on a hash?
by plobsing (Friar) on Dec 08, 2007 at 17:57 UTC
    '@': You are enforcing an order on a hash, effectively making it a list. So now you get to treat it as a list.
    '$': Ordinary hash lookup gets one element of a hash which you get to treat like a scalar.
Re: meaning of '@' sigil on a hash?
by shmem (Canon) on Dec 08, 2007 at 18:18 UTC
    Why do you need the '@' preceding the hash name and not a '$'?

    Because you are accessing the hash in list context. Did you try it with a $ as sigil? Doing so will result in

    $VAR1 = { '3' => 3 };

    because $hash{@keys} is a scalar. So, the array @keys is evaluated in scalar context, which yields 3 (three elements). The scalar context on the left of the assignment enforces scalar context on the right, so that's also 3 (three elements.)

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: meaning of '@' sigil on a hash?
by Burak (Chaplain) on Dec 08, 2007 at 18:28 UTC
    That's called slicing: @hash{@keys} = @values; And this is bogus code: $hash{@keys} = @values; When you want to take a slice, you will end up with a list, so that sigil will not be "$" but "@" both for hash and array slices. Array slice:
    @array[0..4] = qw(a b c d e);
Re: meaning of '@' sigil on a hash?
by ikegami (Pope) on Dec 08, 2007 at 19:19 UTC

    In Perl5, the sigil represents how the variable should be treated. It does not represent the type of the variable. So just like $ can be used by scalar, arrays and hashes, @ can be used by arrays and hashes.

    select elementselect elements (slice)all keysall valuesall keys & values
    scalar$scalar
    array$array[$i]@array[@i](0..$#array)@array
    hash$hash{$i}@hash{@i}keys(%hash)values(%hash)%hash
    list(...)[@i]

    This is all documented in perldata, including slices.

    Update: Reordered the columns.

Re: meaning of '@' sigil on a hash?
by bradcathey (Prior) on Dec 09, 2007 at 00:17 UTC

    awohld, glad to have you around, but help those who will be searching your nodes in the future, by Choosing a Good, Descriptive Title.


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://655850]
Approved by graff
Front-paged by runrig
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2014-10-24 21:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (137 votes), past polls