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

Hello fellow monks, I am still learning this interesting language we call Perl and am looking for some assistance (again). I want to get some information out of a file on the server that I can then check input from a HTML form against. For example if UserX enters "Star" into text field 'INPUT1' and then "Wars" into 'INPUT2', I would then like to check that these inputs match two fields in a Config file. I have made some dummy code below, the problem I am getting is when I call the PARSEINFO package, and specifically when I use Config::IniFiles. I have commented out everything except for the below code in the PARSEINGO package:
#!c:\Perl\bin\perl use warnings; use strict; package PARSEINFO;
and have had no problems. Also in the actual code it all compiles fine. The problem I get is that I receive a Internal Server Error as soon as I uncomment "use Config::IniFiles;". My test code is:
#!c:\Perl\bin\perl use warnings; use strict; use PARSEINFO; use GUI; use CGI qw(:standard); use constant PASS => 1; my $i_input1 = param('INPUT1'); my $i_input2 = param('INPIT2'); my %data; &PARSEINFO::ParseConfigFileToHash( 'c:\inifile.ini', \%data ); if( ($i_input1 eq $data{input1}) && ($i_input2 eq $data{input2}) ) { if( ($i_input2) || ($i_input1) ) { my @args = ("../bin/Script.exe", $i_input1, $i_input2); system(@args); my $result = $? >>8; if ( $result == PASS) { &GUI::HtmlDoc1(); } else { &GUI::HtmlDoc2(); } } else { &GUI::HtmlDoc3(); } } else { &GUI::HtmlDoc2(); } exit (0);
and my PARSEINFO package (PARSEINFO.pm) code is:
#!c:\Perl\bin\perl use warnings; use strict; use Config::IniFiles; package PARSEINFO; sub ParseConfigFileToHash { my $ini_file = shift; my $cfg = Config::IniFiles->new( -file => $ini_file ); my $hashref = shift; $hashref->{input1} = $cfg->val( 'SETTINGS', 'INPUT1'); $hashref->{input2} = $cfg->val( 'SETTINGS', 'INPUT2'); } sub OutputConfigFile{ my $ini_file = shift; my $newConfigFile = Config::IniFiles->new( ); $newConfigFile->SetFileName( $ini_file ); my $hashref = shift; my $sectionName = 'SETTINGS'; $newConfigFile->newval( $sectionName, 'INPUT1', $hashref->{ +input1} ); $newConfigFile->newval( $sectionName, 'INPUT2', $hashref->{ +input2} ); $newConfigFile->RewriteConfig(); $newConfigFile->WriteConfig( $ini_file ); } 1;
I know that the GUI Package works fine and the HTMLDOC1, 2 and 3 all have no problems. Thank you in adavnce for any assistance you can offer.

Replies are listed 'Best First'.
Re: Using Config::IniFiles in CGI
by mr_mischief (Monsignor) on Jan 11, 2011 at 09:48 UTC

    An "Internal Server Error" is typically triggered by printing something before the proper HTTP headers. This often happens, as in your case, when an error happens and some error message is output. You probably have an error loading the module Config::IniFiles when running as the CGI user. This user account is usually the same as the main web server user, like "nobody", "apache", "http", or similar.

    One way to see what is going on is while you're developing your program to use CGI::Carp and more specifically its fatalsToBrowser mode. This allows you to see the error message that's causing the web server to give up on your program by means of injecting a minimal HTTP header in case of errors in the program. It'll just pop the error perl reports right into your browser window over the network as if it was the page you were seeking. That allows you to fix the problem, whatever that may be, and get back to developing the program.

    It's probably a good idea to disable this debugging aid when your program goes to production. The end users don't need to know what modules refuse to load and things of that sort. You can always leave it enabled on your development system and enable it as needed on your testing or staging server.

Re: Using Config::IniFiles in CGI
by Anonyrnous Monk (Hermit) on Jan 11, 2011 at 09:37 UTC
    I receive a Internal Server Error as soon as I uncomment "use Config::IniFiles;".

    Are you sure the module is installed on the web server? What does the web server's error log say?

    You could try putting use CGI::Carp qw(fatalsToBrowser); at the beginning of the program to have the error message be sent to the browser.

Re: Using Config::IniFiles in CGI
by KyussRyn (Acolyte) on Jan 17, 2011 at 05:24 UTC
    To readers of this thread,

    The problem I had was that the server didn't have the correct modules installed. So after installing the Config::IniFiles on the server it worked perfectly.

    To confirm this was the problem I included the line use CGI::Carp qw(fatalsToBrowser); to my "headers" list and it printed out the problems I was facing perfectly when I ran it on the server.

    Thank you to both Mr Mischief and Anonmyous Monk who replied.