Hello,
I have the following situation:
I create a class by means of Class::Base mechanism.

The method init() should be overriden in order to initialize my own object:

sub init { my ($self, $config) = @_; @$self{ keys %$config } = values %$config; $self->{ sql } = $SQL; $self->connect( ) || return; return $self; }

This method it is called internally by the new() constructor, where bless function returns the reference object associating the reference to the anonymous hash with invocant $class, thus all accesses to the internal glob of data will be through the invocant $class.

Next, init() method it is called:

$self->init($config);

This is the new() constructor taken from the source:

sub new { my $class = shift; # allow hash ref as first argument, otherwise fold args + # into hash my $config = defined $_[0] && UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ }; no strict 'refs'; my $debug = defined $config->{ debug } ? $config->{ debug } : defined $config->{ DEBUG } ? $config->{ DEBUG } : ( ${"$class\::DEBUG"} || 0 ); my $self = bless { _ID => $config->{ id } || $config->{ ID } || $class, _DEBUG => $debug, _ERROR => '', }, $class; return $self->init($config) || $class->error($self->error()); }

My question is: What does the following line of code :

@$self{ keys %$config } = values %$config;

Because it seems to dereference an array that is a value in the hash refered to by $self.

But keys %$config, returns an array of keys from the dereferenced hash refered to by $config and values %$config, returns an array of values from the dereferenced hash refered to by $config.

Is this creating pairs such thsi key_i => [value_i] foreach key and value elements?

Could anybody translate for me that line of code, please?

Thank you

20060906 Janitored by Corion: Removed H2, Added formatting, remoded code tags, as per Writeup Formatting Tips


In reply to Hash assignment misunderstanding by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.