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

Hello monks!

I have the following code and iam gettign those errors when index.pl trying to pass values to admin.pl and i dont see why.Also what I dont understand 2 things. Why the scripts dies prematurely although 'perl -c admin.pl' finds it syntactically correct and what is this uninitialized value in string that the error is complaining about.

[Fri May 25 13:06:47 2007] [error] [client 10.0.0.2] Premature end of +script headers: admin.pl, referer: http://dell/ [Fri May 25 13:06:47 2007] [error] [client 10.0.0.2] [Fri May 25 13:06 +:47 2007] admin.pl: Use of uninitialized value in string eq at D:\\ww +w\\cgi-bin\\admin.pl line 26., referer: http://dell/
Here is the code, admin.pl is actually a decission making script on what script will be nextly ran depending on index.pl's form values.
#!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); use DBI; use POSIX qw(strftime); use Encode; my $title = param('title'); my $request = param('request'); my $orderlist = param('orderlist'); my $remark = param('remark'); my $host = param('host'); my $date = param('date'); my $action = param('action'); my $gamefile = param('gamefile'); my $gamename = param('gamename'); my $gamedesc = param('gamedesc'); my $counter = param('counter'); #========================== REDIRECT TO PROPER SCRIPT ================ +========== if( $action eq 'Παραγγελί +α!' ) { #========================= SHOW USER LIST TO PUBLIC ============== +========== if( $title eq 'show users' ) { print redirect("/cgi-bin/show.pl?action=Παρ&#94 +5;γγελία!&request=$request"); exit 0; } #================== SHOW USERS THAT HAVE DOWNLOADED FROM VAULT === +========== elsif( $title eq 'show downloads' ) { print redirect("/cgi-bin/show.pl?action=Παρ&#94 +5;γγελία!&request=$request"); exit 0; } #========================= SAVE USER DATA TO DATABASE============= +========== elsif( defined($title) and defined($request) and defined($orderlis +t) and defined($remark) ) { print redirect("/cgi-bin/show.pl?action=Παρ&#94 +5;γγελία!&authname=$ENV{REMOTE_USER}&ti +tle=$title&request=$request&orderlist=$orderlist&remark=$remark&host= +$host&date=$date"); exit 0; } else { print redirect("/data/private/") } } #========================== AUTHORIZE USER TO DOWNLOAD =============== +========== print redirect("/cgi-bin/vault.pl?authname=$ENV{REMOTE_USER}&gamename= +$gamename") if( defined($gamename) );

Replies are listed 'Best First'.
Re: Premature end of script error
by leocharre (Priest) on May 25, 2007 at 10:31 UTC

    For one I would "use warnings;" - it gives you extra info. Just standard procedure to use that.. never know..

    Also, why dont you check a little more on those values.. just snoop a little.. do this... add..

    use Smart::Comments '###'; # and then after these... my $title = param('title'); my $request = param('request'); my $orderlist = param('orderlist'); my $remark = param('remark'); my $host = param('host'); my $date = param('date'); my $action = param('action'); my $gamefile = param('gamefile'); my $gamename = param('gamename'); my $gamedesc = param('gamedesc'); my $counter = param('counter'); # put this in there ### $title ### $request ### $orderlist ### $remark ### $host ### $date ### $action ### $gamefile ### $gamename ### $gamedesc ### $counter

    Then fire up a terminal emulator (putty (ssh client), or use a real operating system- ) and do # tail -f /var/log/httpd/error_log ( where your server log resides) and when you fire up your cgi, you can see the ouput as it goes. Could be useful.

      For one I would "use warnings;" - it gives you extra info. Just standard procedure to use that.. never know..

      It doesn't give extra info wrt -w, but it is more flexible. (So yours is indeed a good recommendation.)

Re: Premature end of script error
by clinton (Priest) on May 25, 2007 at 10:33 UTC
    It would have been helpful if you had told us which line was line 26, rather than making me count them.

    The error means just what it says : that a variable you are using in an eq comparison has not been initialised, ie it is undefined. Have a look at perldiag for an explanation of this warning.

    I am guessing that that variable is $action.

    You could change your param sub to always return a defined value. For instance:

    sub param { my $key = shift; my $value = ....code to get value...; return defined $value ? $value : ''; }

    Update

    As blazar pointed out below, param() is from CGI, so you could do the following:
    sub get_defined_param { my $value = param(@_); return defined $value ? $value : ''; }
    and replace all your call's to param() as follows:
    my $title = get_defined_param('title'); my $request = get_defined_param('request'); my $orderlist = get_defined_param('orderlist'); my $remark = get_defined_param('remark'); my $host = get_defined_param('host'); my $date = get_defined_param('date'); my $action = get_defined_param('action'); my $gamefile = get_defined_param('gamefile'); my $gamename = get_defined_param('gamename'); my $gamedesc = get_defined_param('gamedesc'); my $counter = get_defined_param('counter');
      You could change your param sub to always return a defined value. For instance:

      It's not his param sub, it's CGI.pm's. Granted, he may write a wrapper around it, but he'd better take care of undefined values where they may matter.

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Premature end of script error
by blazar (Canon) on May 25, 2007 at 12:01 UTC
    Why the scripts dies prematurely although 'perl -c admin.pl' finds it syntactically correct

    Syntactical correctness does not protect one from runtime errors.

    C:\temp>perl -cle "print 1/<>" -e syntax OK C:\temp>perl -le "print 1/<>" 2 0.5 C:\temp>perl -le "print 1/<>" 0 Illegal division by zero at -e line 1, <> line 1.