Reputation: 59
I have .ini file (config file for the database operations):
[Section1]
SQL1=select * from <tablename>
SQL2=insert into table <table name>
I have written below code to read the each section of .ini file and its working perfect. I have to use below subroutine in my mail file, I want to call it and pass the each value in each section into the hash over there and do the database operations.
below is code:
sub Read_INI_files_get_initialData {
my ( %ini_file, $ini_sect );
tie %ini_file, 'IniFiles',( -file => "/home/testtool/config/InitialData.ini" );
for $ini_sect ( keys %ini_file ) {
%$ini_sect = %{ $ini_file{$ini_sect} };
}
print "$Section1{SQL1}\n"; # output prints the 1st SQL1 statement return in .ini file.
return (\%Section1);
}
When I call this subroutine from main file, I don't get any return value which I could use for further database opration.
Upvotes: 0
Views: 131
Reputation: 65
If I understand you correctly, you want to use the tied hash to access the SQL statements specified in your config file. This can be done by using the nested hash structure $ini_file{SectionName}{VariableName}
:
use strict;
use warnings;
use Config::IniFiles;
my %initialData = Read_INI_files_get_initialData();
print $initialData{Section1}{SQL1} . "\n"; # Prints the Section1 SQL1 statement from .ini file.
sub Read_INI_files_get_initialData {
my %ini_file;
tie %ini_file, 'Config::IniFiles', ( -file => "InitialData.ini" );
return %ini_file;
}
Upvotes: 1
Reputation: 54323
You have a mixup with your variables. Also I'm not sure what you are trying to do. If you only want to read Section1
, consider this example (which I have not tested).
use strict;
use warnings;
use feature 'say';
sub Read_INI_files_get_initialData {
tie my %ini_file, 'IniFiles',( -file => "/home/testtool/config/InitialData.ini" );
say "$ini_file{Section1}->{SQL1}";
# return a hashref
return { $ini_file{Section1} };
}
Basically what you did was the following:
for $ini_sect ( keys %ini_file ) { %$ini_sect = %{ $ini_file{$ini_sect} }; } print "$Section1{SQL1}\n"; # output prints the 1st SQL1 statement return in .ini file. return (\%Section1);
The $ini_sect
is declared above, but then you use it to iterate over the keys. So the first time the for
is run, it will get a key of %ini_file
. Now in the loop you asume it is actually a hashref, dereference it and assign another hash (which you dereferenced from a hash ref using the key). There are two issues here.
First, you are overwriting the variable that holds the key. In the next iteration, that value would be gone.
Second, and more important, you are trying to dereference a string. That won't work. If you add use strict
and use warnings
to you program (as I did above), it will tell you Can't use string ("Section1") as a HASH ref.... So there lies another problem.
What it will also tell you is that Global symbol "%Section1" requires explicit package name in the return, because you never declared it.
Think about what you want to do in your function. Use as many variables as you need, and give them meaningful names. Do you just want to read the first section of the file? Go ahead, reference it directly.
Do you want to make a copy of the whole thing? Maybe tie
is not the best option. See Config::IniFiles how to do it with an OOp interface.
Upvotes: 1