Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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!

In reply to hash deref confusion: Intermediate Perl by live4tech

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2023-03-23 17:50 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.