Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: hash deref confusion: Intermediate Perl

by Anonymous Monk
on Jul 30, 2012 at 06:40 UTC ( [id://984362]=note: print w/replies, xml ) Need Help??


in reply to hash deref confusion: Intermediate Perl

But, in the next line, "@$self" is syntax for dereferencing an array reference, not a hash reference!

Its both; to quote myself:

This is called a hash slice , and in a quirk of perl syntax uses the @ sigil instead of %

my %foo; @foo{ 'hash', 'slices' } = ( 'use', 'curly braces' );

my @bar; @bar[ 1,2,3,4 ] = ( 'array', 'slices', 'use', 'square brackets' );

See also References quick reference

  • Comment on Re: hash deref confusion: Intermediate Perl

Replies are listed 'Best First'.
Re^2: hash deref confusion: Intermediate Perl
by tobyink (Canon) on Jul 30, 2012 at 07:03 UTC

    I wouldn't really say it's a quirk. You're assigning a list of values to a list of hash slots, so the @ sigil is quite appropriate.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      I wouldn't really say it's a quirk. You're assigning a list of values to a list of hash slots, so the @ sigil is quite appropriate.

      Sure, from that perspective it makes sense, but IIRC TheLarry himself called this a mistake, its why perl6 always calls a %hash a  %hash{$key} not  $hash{$key}

        That's not specific to slices though. In Perl 6 sigils are not context-dependent; they are variable-dependent and context-invariant. In Perl 5, where sigils are used to indicate context, using @ for hash slices is pretty consistent with the rest of the language.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^2: hash deref confusion: Intermediate Perl
by live4tech (Sexton) on Jul 30, 2012 at 14:09 UTC
    Thank you! This is a very clear and concise answer. I find it difficult to remember all the syntax in perl (also true especially of all the special symbols $....

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-16 17:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found