nisha has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks, I have a problem with the Config::IniFiles module that i am using. I have an ini file which has to be read section by section; and i have written a small perl code for the same using this module.
!/usr/bin/perl use Config::IniFiles; my $cfg = new Config::IniFiles( -file => "C:/test.ini" ); #Retrieves all the sections in the ini file. @secs= $cfg->Sections(); foreach $i (@secs) { #Retreives all parameters in the section @params = $cfg->Parameters($i); #For each parameter in a particular section print #out the val +ue callme (@params,$i); } sub callme { @params = @_; $sec = $_; foreach $i (@params) { chomp($i); chomp($sec); #This below statement is not printing out the values. print $cfg->val ($sec , $i); } }
The statement $cfg->val ($sec , $i); does not print out the value; However on using
print $cfg->val(Section1,Color)
i.e directly specifying the section value and the parameter value the values for the same are getting printed but the same not working if variable names are used. Could you please help me solve this problem. Thanks, Nisha

2006-02-16 Retitled by Arunbear, as per Monastery guidelines
Original title: 'Config::IniFiles.'

Replies are listed 'Best First'.
Re: Getting sections with Config::IniFiles
by holli (Abbot) on Feb 16, 2006 at 10:36 UTC
    The problem lies in this snippet:
    callme (@params,$i); sub callme { @params = @_; $sec = $_; #... }
    That's because all parameters passed to a sub end up beeing members of @_. What you need is to specify the scalar first or use references.
    Do it like so:
    callme ($i, @params); sub callme { my $sec = shift @_; my @par = @_; }
    or
    callme (\@params, $i); sub callme { my @par = @{$_[0]}; my $sec = $_[1]; }
    or
    callme (\@params, $i); sub callme { my @par = @{shift @_}; my $sec = shift @_; }
    Whatever you like better. All this is explained in perlsub and perlref.

    Update: fixed typos


    holli, /regexed monk/
      Thank You very much. It helped.