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

I have written below code and wana add in the module
sub Read_INI_files_get_initialData{ my ( %ini_file, $ini_sect ); tie %ini_file, 'IniFiles', ( -file => "/home/testtool/config/InitialData.ini" ); foreach $ini_sect ( keys %ini_file ) { %$ini_sect = %{ $ini_file{$ini_sect} }; }
Return a above subroutine,which is working perfectly, but I want to return ini files' section in to hash, but it is giving error "Read_INI_files.pm did not return a true value" My Ini file contains below configuration:
[section1] SQL1 = select * from .... SQL2 = Insert into table <tablename> and so on.
what and how should I return value to hash.

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

    It is the module that needs to return a true value. You did not show us the module. Most likely you can fix the problem by adding a 1; at the end of the code part of the module.

Re: Return a value
by vinoth.ree (Monsignor) on Jul 06, 2015 at 07:16 UTC
    ++Corion

    If you forget the 1 at the end of a package, Perl tells you "The package didn't return a true value".

    For more details, refer this node, Perl Module ending without 1;


    All is well. I learn by answering your questions...
      Thanks both of you... Module is working now...I added return 1 previously as well.. somehow deleted.. Thanks again...
Re: Return a value
by chacham (Prior) on Jul 06, 2015 at 14:30 UTC

    Side comment: "select *" is open to bugs, if the column order changes. Adding the column names is also a little self-documenting.

    Overall, this looks suspiciously like dynamic SQL, which is not very secure.

      True dat. Take the $time to type the extra characters now and you will save ($time * 10) in debugging later.

      Especially now that you have discovered a good way to keep your parameters in an external ini file. The lines in that file are free! You should fill that file up with one entry per SQL statement and use the right one for each task.

      As the poster above said, making dynamic SQL statements is as dangerous as making soft references (variable names made from variable values in your program).

      The only thing that should be dynamic in your SQL statements is the bind placeholder(s).

      Slow down, and do it right!

      Remember: Ne dederis in spiritu molere illegitimi!
Re: Return a value
by 1nickt (Canon) on Jul 06, 2015 at 09:45 UTC

    Hi Bhushan,

    I lost enough tiny little 1;s that I now write it with a comment to myself like this:

    package Foo; 1; # return true to end package Foo

    A little harder to accidentally delete like that!

    Remember: Ne dederis in spiritu molere illegitimi!
      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)

        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.

        A reply falls below the community's threshold of quality. You may see it by logging in.