http://qs1969.pair.com?node_id=911156

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

Hello Monks,

I have a Perl-CGI code, and when it's run, everything looks fine. But I checked out the error_log and found:

[Thu Jun 23 16:28:09 2011] [error] [client ::1] Use of uninitialized value $c_enabling in string eq at /Library/WebServer/CGI-Executables/myfulltextsearch2011.cgi line 53, <IRVF> line 289.

This was caused by this line of code:

my $c_enabling = param('enabling') ... if ($c_enabling eq 'on')

I got rid of it by putting this line in between:

$c_enabling='';

Which is obviously not practical (in the case that the param was chosen). How can I permanently fix this? Is it part of the HTML?

Replies are listed 'Best First'.
Re: Getting rid of an uninitialized warning
by Sandy (Curate) on Jun 23, 2011 at 20:50 UTC
    Replace
    my $c_enabling = param('enabling');
    with
    my $c_enabling = param('enabling') || '';
    If $c_enabling can never have '0' as a valid input;

    otherwise

    my $c_enabling = param('enabling'); $c_enabling = '' unless defined $c_enabling;

      Thanks a lot, that was the problem, it was aloud to have a 0 input, but I didn't know how to check that easily. I had tried the defined check, but it didn't work. I really hope this can't backfire, since I just put it everywhere...

Re: Getting rid of an uninitialized warning
by ikegami (Patriarch) on Jun 23, 2011 at 21:03 UTC

    Correct me if I'm wrong, but I believe enabling can either be present with value 'on' or absent.

    If so, the provided solutions are overkill and all you need is

    my $c_enabling = param('enabling') ... if ($c_enabling)

      Ah, I see, just check if there is a value there. Genius.

      Thanks!

      Re-phrased Question: Are there any possible negative outcomes from using the methods presented by others, where the conditional statement includes "the defined -or operator"?

        I don't understand the question.
Re: Getting rid of an uninitialized warning
by ww (Archbishop) on Jun 23, 2011 at 20:56 UTC

    What you've told us leaves me less than confident that I understand what the actual problem is. See I know what I mean. Why don't you?. But here are a few WAGs/WAIs:

    • Make the parameter required (since it appears the log tells you that some visitor missed a checkbox, input field or some such) in the html.
    • Consider using CGI::Validate or Params::Validate
    • Move your $c_enabling=''; above (eg, before) the code you've shown (and move the my to the same line and put a test of the parameter's existance ahead of line 1 along code to tell the user than the param must be supplied, at which point $c_enabling won't be uninitialized and the visitor will be cautioned against omitting a field.
    Of course, if I've mis-diagnosed your problem, the above may be mere garbage....

      Sorry, I will try and be more clear from now on. Thanks for letting me know.

Re: Getting rid of an uninitialized warning
by wind (Priest) on Jun 23, 2011 at 20:55 UTC