in reply to Re^3: Return a value
in thread Return a value

Sure.. below is the code, where I am trying to call the subroutine written in the module.
#!/usr/bin/perl use DBI; use DBD::Sybase; use strict; #use Config::IniFiles; require Read_INI_files; require IniFiles; #my %Section1; #Connect to the PP_DB my $dbh = DBI->connect( "dbi:Sybase:server=ctp-1-zone157; database=PP_ +DB; port=5000","sa", "password" ) or die "can not connect to the DB"; Read_INI_files_get_initialData_PP_DB(); print "$Section1{SQL1}\n";

Replies are listed 'Best First'.
Re^5: Return a value
by 1nickt (Canon) on Jul 06, 2015 at 11:13 UTC

    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!

    Remember: Ne dederis in spiritu molere illegitimi!