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

Hi Everybody, Below code I am trying.
use warnings; use Config::IniFiles; sub readtestcase { my $ConfigFile = $_[0]; tie %ini_file, 'Config::IniFiles', ( -file => $ConfigFile ); foreach $ini_sect ( keys %ini_file ) { %$ini_sect = %{ $ini_file{$ini_sect} }; print "$ini_sect\n"; while( my( $key, $value ) = each %$ini_sect ) { print "$key: $value\n\n"; } return ( \%TEST_CASE, \%PRE_CONFIG, \%PROCESS_RESTART, \%ABCD, \%POST_CONFIG, \%DB_VALIDATION, ); } } readtestcase("file1.txt");
while running this output coming like:: not able to return the hash reference of sections: whats wrong in this ??
Name "main::TEST_CASE" used only once: possible typo at ./file.pl line + 29 Name "main::PRE_CONFIG" used only once: possible typo at ./file.pl lin +e 30 Name "main::ABCD" used only once: possible typo at ./file.pl line 31 Name "main::PROCESS_RESTART" used only once: possible typo at ./file.p +l line 32 Name "main::POST_CONFIG" used only once: possible typo at ./file.pl li +ne 33 Name "main::DB_VALIDATION" used only once: possible typo at ./file.pl +line 34

Replies are listed 'Best First'.
Re: returning the hash reference
by Eily (Monsignor) on Mar 15, 2016 at 09:34 UTC

    Perl is telling you that it is weird that you are using the variables %TEST_CASE, %PRE_CONFIG and the others at one place, while they are not present anywhere else. So it looks like you have forgotten to declare them, or made a typo (which is what the warning literally says). Warnings are a very good safety net, so unless you are very confident that you understand all the implications, you should NOT disable them. In this case perl is trying to protect you against typos, and other warnings will help you avoid most of your mistakes.

    You are using warnings, which is a good thing. You should also use strict which is the extra layer of safety that will make you avoid the most common pitfalls. They are not arbitrary rules, where some people thought it was nicer to do it one way rather than another (that's the contrary of perl philosophy), warnings and strict have been designed to avoid the most common of errors, and the harder to track bugs.

    Here you are using symbolic reference, ($ini_sect as a variable name) which is not allowed under strict. The reason for that is that it modifies variables in the background (you don't always know which ones, because the soft doesn't always contain the values you expect), that perl has no control over. Variables declared with my on the other hand are easy for perl to follow, because it knows where they belong (less risk of using a variable in a place where you haven't computed its content), when to get rid of it (better memory management), and exactly how their names are written. But you don't need that, since all the hashes you want are already available has values of your tied hash:

    tie %ini_file, 'Config::IniFiles', ( -file => $ConfigFile ); return ($ini_file{TEST_CASE}, $ini_file{PRE_CONFIG}, $ini_file{PROCESS +_RESTART}); # or just return (@ini_file{ "TEST_CASE", "PRE_CONFIG", "PROCESS_RESTA +RT" });
    You might want to check that every key exists before returning them.

Re: returning the hash reference
by Discipulus (Canon) on Mar 15, 2016 at 08:59 UTC
    You can easely escape this error, if it is what you want and you understand it, using no warnings qw(once);

    See warnings for more details. Or you can declare such hashes before setting their values from INI file.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      If I use  no warning;, no use actually... It only removes the compilation errors but while de-referencing the hash reference it will only does for 1st one...

        You should never remove a warning if you don't understand it perfectly, even if the code still seems to be working properly, it might be doing some extra things that you don't see, or skipping some steps.

        Here the problem is that return can happen only once in a function, has it exits it and returns (obviously) the result straightaway. So your for loop is only called for one key of the tied hash, and all the other iterations never have a chance to be ran.

Re: returning the hash reference
by Marshall (Canon) on Mar 15, 2016 at 21:38 UTC
    I can't easily install Config:iniFiles on my Active State machine for testing. You obviously know which sections of the .ini file that you are interested in.

    Have you considered using an easier module, say Config::Tiny? If all you need to do is to read the sections and the variables (the most common case), do it an easier way. The module that you have can do a lot of stuff that maybe you don't need?