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

I am passing my .cgi perl program a value called loc via GET, to indicate which files it should patch together and display. I want to cover the instance where no name=value is give - say someone just loads up "myperl.cgi" without the "?loc=value".

Can someone tell me what $locvar = $query->param('loc') will produce if no actually loc is passed? I've read that it will return undef but I cant figure out how to check for that, and I've also read that it will return a null '' but I haven't gotten that to work either.

Replies are listed 'Best First'.
Re: check for undefined param name
by tadman (Prior) on Apr 02, 2002 at 21:22 UTC
    You can check for undef simply by using the defined function:
    if (!defined ($query->param('loc'))) { $user->thwap(); }
    Of course, what kind of action you take is up to you.
      We should also check whether it has a length in this case, since it could be the empty string (e.g., if loc=&foo=bar is passed).
Re: check for undefined param name
by CukiMnstr (Deacon) on Apr 02, 2002 at 21:40 UTC
    and I've also read that it will return a null '' but I haven't gotten that to work either.

    a null '' is actually undef, it equals to false, so

    unless ($query->param('loc')) { # print error mesage or whatever. }
    would work, but

    if ($query->param('loc) eq '') { ... }

    does not work...

    you might want to take a look at What is truth? here at the monastery for an explanation of "truth" in perl, as there are many 'gotchas' in handling truth in perl ;) (for example, in the code above, if 0 was a valid value for the loc parameter, the code would print the error message, even if we didn't want that.)

    hope this helps,

      As was pointed out, it might be better to check using length instead of defined. Remember that zero is false, so a straight logic check will not tell you if it is supplied but zero. Consider:
      unless ($query->param('is_a_chicken')) # Fails on 0 { print "You must choose an option.<BR>\n"; }
      As your appropriate values might be 0 or 1, this is going to fail on a valid submission. Better to have:
      unless (length $query->param('is_a_chicken')) { print "You must choose an option.<BR>\n"; }
      Though length does tend to complain about being forced to deal with undefined strings. This will lead to the more verbose but equally more robust version:
      my $is_a_chicken = $query->param('is_a_chicken'); unless (defined ($is_a_chicken) && length ($is_a_chicken)) { print "You must choose an option.<BR>\n"; }
      Of course, if you were doing this sort of thing all the time you could make a quick helper method to do it quickly:
      sub provided { return defined($_[0]) && length ($_[0]); }
      Which would simplify as follows:
      unless (provided ($query->param('is_a_chicken')) { print "You must choose an option.<BR>\n"; }
Re: check for undefined param name
by thunders (Priest) on Apr 02, 2002 at 23:21 UTC
    if you are simply looking for what to do if you get no parameters or unexpected parameters,
    if(!$query->param){ do_stuff(); }elsif($query->param('loc')){ do_something_with($query->param('loc')) }else{ error_page(); #for invalid parameters }

    is another way