live4tech has asked for the wisdom of the Perl Monks concerning the following question:
Almost through the Intermediate Perl book and came across code I do not understand and that was not explained clearly (seems to happen to me frequently in this book!). Here is the code:
sub named { #class method my $self = shift->SUPER::named(@_); my $name = $self->name; my @standings = split ' ', $STANDINGS{$name} || "0 0 0 0"; @$self{qw(wins places shows losses)} = @standings; $self; }
As background: $self is a blessed hash reference. The method 'name' returns a scalar string. %STANDINGS is a hash that is associated with a DBM file.
My question: I do not understand the line @$self{qw(wins places shows losses)} = @standings;
I see that @standings becomes a list of 4 numbers as a result from splitting on spaces. But, in the next line, "@$self" is syntax for dereferencing an array reference, not a hash reference! Further, the use of the "{" after this indicates a hash, but I have never seen a hash defined in this way. Are they actually saying that you can have a list of keys ("qw(wins places shows losses)") and set the values by setting this to the list "@standings"?? Really?
PERL always seems to confound me with syntax! And when I cannot find the answer in perldocs etc. I just thorw up my hands and post it here!
THANK YOU for being there monks!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: hash deref confusion: Intermediate Perl
by davido (Cardinal) on Jul 30, 2012 at 06:38 UTC | |
Re: hash deref confusion: Intermediate Perl
by BrowserUk (Patriarch) on Jul 30, 2012 at 06:41 UTC | |
Re: hash deref confusion: Intermediate Perl
by Anonymous Monk on Jul 30, 2012 at 06:40 UTC | |
by tobyink (Canon) on Jul 30, 2012 at 07:03 UTC | |
by Anonymous Monk on Jul 30, 2012 at 08:13 UTC | |
by tobyink (Canon) on Jul 30, 2012 at 08:30 UTC | |
by Anonymous Monk on Jul 30, 2012 at 08:37 UTC | |
by live4tech (Sexton) on Jul 30, 2012 at 14:09 UTC | |
Re: hash deref confusion: Intermediate Perl
by AnomalousMonk (Archbishop) on Jul 30, 2012 at 13:35 UTC | |
by live4tech (Sexton) on Jul 30, 2012 at 14:03 UTC |