in reply to Using config::any

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

Replies are listed 'Best First'.
Re^2: Using config::any
by ItsyBitsy (Novice) on May 31, 2012 at 03:02 UTC
    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
        Thanks for the help. I probably should have pasted the output which i recieved. Will do so from here onwards.