Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

null values

by fluffyvoidwarrior (Monk)
on May 26, 2005 at 07:40 UTC ( #460537=perlquestion: print w/ replies, xml ) Need Help??
fluffyvoidwarrior has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, Using Activestate perl, Apache 2and CGI package. I'm returning variable values from web page form user input. eg - $myvariable = $q->param( "myinput" ); I then test for a null value with eg - if ($myvariable eq ""){ #ie if user didnt fill in field exit; #don't try to work on a null value } Although my scripts work ok I'm filling up my apache error log with drivel like - "...Use of uninitialized value in string eq at ..." It seems that an uninitialised variable is not the same as a null variable. How do I test for an uninitialised variable? I've hunted through quite a lot of big fat perl books to no avail. This seems like an embarrassingly dumb question, sorry. Masters, you are my only hope .... Thanks

Comment on null values
Re: null values
by robartes (Priest) on May 26, 2005 at 07:45 UTC
    perldoc -f defined


Re: null values
by monarch (Priest) on May 26, 2005 at 08:08 UTC

    This has to be one of the most common initial questions that comes up about perl.

    if ( defined( $q->param( "myinput" ) ) ) { print( $q->param( "myinput" ) ); }


    my $value = $q->param( "myinput" ); print( $value ) if ( $value );

    The above only works if $value is never going to be zero, because zero evaluates to false. A value of undef or zero is false. The following is safer:

    my $value = $q->param( "myinput" ); print( $value ) if ( defined( $value ) );
      Thanks People
Re: null values
by polettix (Vicar) on May 26, 2005 at 12:10 UTC
    I sometimes find it useful to define a wrapper function to set a default value:
    sub defaulted_param { my ($param_name, $default_value) = @_; return $default_value unless defined $q->param($param_name); return $q->param($param_name); }
    (assuming $q is global, of course).

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.

      For future reference, I believe, if I can remember right, that usually when $q->param($param_name); is used, a person is using mod_perl. In this case, you should always pass $q to the sub as well, otherwise bad things tend to happen (at least they always did for me!).


Re: null values
by LanceDeeply (Chaplain) on May 26, 2005 at 17:09 UTC
    no one mentioned my personal favorite: setting a default value using the or operator...
    $myvariable = $q->param( "myinput" ) || ""; if ($myvariable eq "") { # no warnings }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://460537]
Approved by pelagic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2015-03-06 00:32 GMT
Find Nodes?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:

    Results (156 votes), past polls