Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

module reference is used in a confusing way

by gwhite (Friar)
on Nov 07, 2008 at 19:01 UTC ( #722271=perlquestion: print w/replies, xml ) Need Help??
gwhite has asked for the wisdom of the Perl Monks concerning the following question:

Going through some code someone else wrote and found this and I don't understand it. Can someone shed some light?

#this creates a new $config thing my $config = new ConfigReader::Simple($config_file); #this checks for a true in one of the elements of the $config if ($config->adv_conf){ ... } #this creates an array out of a list of words my @batch_list = split /\s+/, $config->batch_list; #the next line puts a reference to the array into the $config #but why the $$ and no -> $$config{batch_list} = \@batch_list; #could it be done just as easily with this $config->set(batch_list, \@batch_list); #I am thinking probably not since the doc says: #set( DIRECTIVE, VALUE ) #Sets the value for DIRECTIVE to VALUE. The DIRECTIVE need not already + exist. This overwrites previous values. #The VALUE must be a simple scalar. It cannot be a reference. If the V +ALUE is a reference, the function prints a warning and returns false.

and what are the proper ways to access that $config element?
my @list = @{$$config{batch_list}};
my @list = @{$config->batch_list};


Replies are listed 'Best First'.
Re: module reference is used in a confusing way
by kennethk (Abbot) on Nov 07, 2008 at 19:41 UTC
    $$config{batch_list} = \@batch_list

    This stores a reference to the @batch_list array in the 'batch_list' element of an anonymous hash. The reference to the anonymous hash is stored in $config. If you want to access the stored array, it can be read as

    my @list = @{$$config{batch_list}}


    my @list = @{$config->{batch_list}}

    as well, I'm sure, as a half dozen other ways.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://722271]
Approved by Joost
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2018-06-25 07:00 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.