Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
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 '$'?

Replies are listed 'Best First'.
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 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 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 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?
[ambrus]: ah, it's one of thos
[Eily]: what, there's a difference between proof of concept and production?
[LanX]: 20 years ago traders were complaining about the latency of the trading system...
[ambrus]: I'm currently in the process of rewriting my proof of concept programs. They sort of developped organically as I was experimenting, so now I've got an ugly mess of multiple programs and one-liners held together by nothing. I'll have to rewrite them to som
[ambrus]: ething that's both cleanly organized and mostly automated.
LanX in train, bad connection
[Corion]: ambrus: Yeah - we're in that situation too, except that there is no time to do the reorganizing :-/
[LanX]: ... so my boss started a project with the newest sun servers and invited the traders to come on weekend to test it... and they were so pleased, that they forced him to keep it in production...
[ambrus]: Corion: sure, this is the long-term plan. The short term is that I have to run this ungodly mess to get results from the new input data today.
[Corion]: ambrus: Most of our "automation" is tied to process exit codes and a shell pipeline :-\

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (16)
As of 2017-03-29 11:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (350 votes). Check out past polls.