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

Hi guys, I am using config::any to get the password from my ini file.The ini file looks like this
[password] password=mypass
The code i am using is this:
use strict; use warnings; use Config::Any; my @files; $files[0]="C:\\wamp\\www\\password.ini"; my $cfg = Config::Any->load_files({files =>\@files,use_ext => 1 }); print $cfg;
But the above code simply prints an array reference. How do i get the password stored in my ini file.

Replies are listed 'Best First'.
Re: Using config::any
by kcott (Archbishop) on May 30, 2012 at 02:42 UTC

    Data::Dumper is useful for investigating unknown data structures.

    Dummy ini file for testing:

    $ cat > pm_config_any.ini some_key=qwerty password=Pa55w0rd other_key=zxcvbn

    Code to see full $cfg structure and the specific item you want:

    $ perl -Mstrict -Mwarnings -E ' use Config::Any; use Data::Dumper; my $pw_ini_file = q{./pm_config_any.ini}; my @files = ($pw_ini_file); my $cfg = Config::Any->load_files({files => \@files, use_ext => 1}); say q{Full $cfg structure:}; say Dumper $cfg; say q{Password: }, $cfg->[0]{$pw_ini_file}{password}; ' Full $cfg structure: $VAR1 = [ { './pm_config_any.ini' => { 'password' => 'Pa55w0rd', 'some_key' => 'qwerty', 'other_key' => 'zxcvbn' } } ]; Password: Pa55w0rd

    -- Ken

      Your code worked for the above .ini file when i added use feature "say". However for my password.ini file it still prints the password as a reference. (only the last line though). Any idea why this is happening? Also how would i store this into a string.
      say q{Password: }, $cfg->[0]{$pw_ini_file}{password};
        Your code worked for the above .ini file when i added use feature "say".

        It sounds like you've got a handle on that. feature shows other ways to achieve this: you may have read that already.

        However for my password.ini file it still prints the password as a reference. (only the last line though). Any idea why this is happening?

        In this type of situation, you should show the output you received. The page "How do I post a question effectively?" explains this along with other useful advice: I strongly recommend you read it. Not doing so is likely to result in responses like "Sorry, I don't have the Mental::Telepathy module installed on my system.". :-)

        I'll guess your output looked something like:

        Full $cfg structure: $VAR1 = [ { 'C:\wamp\www\password.ini' => { 'password' => { 'password' => ' +mypass' } } } ]; Password: HASH(0x7f9a03843da8)

        Side issue: with a double-quoted string you need to escape all the special characters, with a single-quoted string you don't. Compare "C:\\wamp\\www\\password.ini" with 'C:\wamp\www\password.ini'. Take a look at Quote-Like Operators in perlop for more details.

        Using a section header (e.g. [password]) adds an additional level to your data structure. Using this ini file:

        [password] password=mypass [other_section] other_key=other_value

        I get this output:

        Full $cfg structure: $VAR1 = [ { './pm_config_any.ini' => { 'other_section' => { 'other_key +' => 'other_value' }, 'password' => { 'password' => ' +mypass' } } } ];

        So, to get the value you're after you'll need:

        say q{Password: }, $cfg->[0]{$pw_ini_file}{password}{password};
        Also how would i store this into a string.

        That's a basic assignment - you don't need to do anything special:

        my $ini_password = $cfg->[0]{$pw_ini_file}{password}{password}; say $ini_password;

        -- Ken

        p in
Re: Using config::any
by NetWallah (Canon) on May 30, 2012 at 02:42 UTC
    This works, but I'd suggest looking for a better way:
    use strict; use warnings; use Config::Any; my @files = ("test.cfg.ini");# $files[0]="C:\\wamp\\www\\password.ini" +; my $cfg = Config::Any->load_files({files =>\@files,use_ext => 1 }); for my $c (@$cfg){ for my $f (keys %$c){ print "File $f has the following :\n"; for my $section(keys %{$c->{$f}}){ print " in Section $section:\n"; for my $k2 (keys %{$c->{$f}{$section}}){ print "\t$k2=" . $c->{$f}{$section}{$k2} . "\n"; } } } } }
    If you wanted to hard-code it, you could write:
    print $cfg->[0]{'test.cfg.ini'}{password}{password} . "\n"; # prints +'mypass'

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      I used the following code(hard coded)
      use strict; use warnings; use Config::Any; my @files = ("C:\\wamp\\password.ini"); my $cfg = Config::Any->load_files({files =>\@files,use_ext => 1 }); print $cfg->[0]{'password.ini'}{password}{password} . "\n"; # prints
      However it throws an error saying
      Use of uninitialized value in concatenation (.) or string at caller.pl + line 39.
        You can use the for-loops in my earlier message to figure out what it is expecting, but I suspect it is something like this:
        print $cfg->[0]{"C:\\wamp\\password.ini"}{password}{password} . "\n" +; #

                     I hope life isn't a big joke, because I don't get it.
                           -SNL

Re: Using config::any
by thomas895 (Deacon) on May 30, 2012 at 04:04 UTC

    You guys are making this all far more difficult than it needs to be.

    use strict; use warnings; use Config::Any; #By the way, load_files is only useful for multiple files, in which ca +se you will have to change the code below. For only one file, load() +is good enough. my $cfg = Config::Any->load( "C:\\wamp\\www\\password.ini" ); #Simply printing $cfg will print its reference, like you asked for. #Config::Any returns a hashref(when used with [cpan://Config::Any::INI +] which interfaces to [cpan://Config::Tiny]), so you need to access i +t like one. print $cfg->{password}->{password};

    The docs for Config::any can be a bit confusing for the beginning programmer, I know.

    ~Thomas~
    bless( $you ) if $you->{sneezed};
      Thanks for the reponse. But when i try load it says
      Can't locate object method "load" via package "Config::Any" at caller. +pl line 15
      Also is there any way to upvote answers which you feel are good like in stackoverflow?

        load() is not a documented method of Config::Any. Developers provide an interface for you to interact with their modules (e.g. Config::Any's Interface) which you can generally expect to be stable across versions of a module: these are the only methods you should use. They may write other methods for the implementation of their modules: these may change at any time without notice. Perhaps load() was such a method at one time.

        Voting/Experience System explains about voting - there's a link to this in the panel on the right-hand side in the Information section.

        -- Ken