in reply to Re^4: Return a value
in thread Return a value
You need to capture the return value!
Your sub returns a hashref, so you have to capture the return value into a hashref in your main program. Right now you are not assigning the return value to anything. (Looks like you tried a hash and it didn't work because you were getting a hashref.) If you want to use it as a hash in your program, you must dereference it. (Although I would keep it as a hashref.)
my $return_value = Read_INI_files_get_initialData_PP_DB(); my %Section1 = %$return_value; print "$Section1{SQL1}\n";
But really, I would find and use a CPAN module for common tasks like this. It is a good practise exercise to write your own sub, but it is also "reinventing the wheel." You should put your limited time and efforts into writing your application code; that which is NOT available on CPAN!
Here's what you could do instead, using your same .ini file. No need to write or maintain a separate sub for getting your config information:
#!/usr/bin/env perl use strict; use warnings; use Config::Tiny; my $ini_file = '/home/testtool/config/InitialData.ini'; my $ini = Config::Tiny->read($ini_file) or die; my $Section1 = $ini->{'Section1'}; my $SQL1 = $Section1->{'SQL1'}; # or ... my $SQL2 = $ini->{'Section1'}->{'SQL2'}; print "SQL1: $SQL1\n"; print "SQL2: $SQL2\n";
I urge you to try this, not just look at it. Install Config::Tiny, copy this script to your test folder, and try it!
|
|---|