http://www.perlmonks.org?node_id=200565

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

Hi guys, I've got a script to calculate usage for a group of my ISP customers. When I run the script at the command line it gives me the error: Use of ininitialized value at ./example.cgi line 28

the line in the code reads this: if ($cgi->param('Submit') eq "Go") {&do_results($cgi); } Can anyone please shed some light on this for me? Im a complete newbie so please dont confuse me too much :-)

Replies are listed 'Best First'.
Re: Help with uninitialized value warning
by tadman (Prior) on Sep 25, 2002 at 07:28 UTC
    You should always test with data that should work. For example:
    % perl example.cgi 'Submit=Go'
    The other case is to be paranoid:
    my $submit = $cgi->param('Submit'); if (defined($submit) && $submit eq 'Go') { do_results($cgi); }
    This is probably a better idea, since someone can easily load your page sans-parameters, and if this warning were actually an error, they would get the old "500 Internal Server Error" page and then complain that your "program doesn't work" even though they are unintentionally feeding it bad data.

    I'd like to take this moment to encourage you to not use ampersands on function calls. The generally accepted way of making a call is bare.

    Update: Changed submit=1 to the appropriate Submit=Go per dws's suggestion. Nothing like having test values that are actually accurate.
Re: Help with uninitialized value warning
by dws (Chancellor) on Sep 25, 2002 at 05:25 UTC
    You're getting the warning because when you invoke the CGI from the command line, you're not defining the form variable Submit. A quick workaround is to change   if ($cgi->param('Submit') eq "Go") ... to
    my $submit = $cgi->param('Submit') || ""; if ($submit eq "Go") ...
    This ensures that you'll be testing against a defined value.

Re: Help with uninitialized value warning
by Fastolfe (Vicar) on Sep 25, 2002 at 16:22 UTC
    Just ignore it, or check to see if $cgi->param('Submit') has a true or defined value first.