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

Hi, I am stuck with one more problem here:
[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 main 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/Initia +lData.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 operations ( I want those SQL queries in hash so I could pass them to insert into my database using another program)

Replies are listed 'Best First'.
Re^3: Return a value
by Corion (Patriarch) on Jul 06, 2015 at 10:29 UTC

    How do you call it and how do you try to extract the returned value(s)?

    Please show us the code where you call the subroutine.

      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";

        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!
    A reply falls below the community's threshold of quality. You may see it by logging in.