Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hash assignment misunderstanding
by wfsp (Abbot) on Sep 06, 2006 at 09:14 UTC | |
by Anonymous Monk on Sep 06, 2006 at 09:39 UTC | |
by nevyn (Monk) on Sep 06, 2006 at 16:14 UTC | |
by hardburn (Abbot) on Sep 06, 2006 at 16:35 UTC | |
by wfsp (Abbot) on Sep 06, 2006 at 16:43 UTC |