Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

Replies are listed 'Best First'.
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" ) ); }

    or

    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 robartes (Priest) on May 26, 2005 at 07:45 UTC
    perldoc -f defined

    CU
    Robartes-

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!).

          -Bryan

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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2023-12-07 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (32 votes). Check out past polls.

    Notices?