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

I want to pass a key/value from a hidden form tag. I need to be able to check if this parameter was passed or not. How can I check to see if this parameter is passed without causing an error? For example, say I have the following code:

<input type="hidden" name="verify" value="yes">

Now lets say there is a script called process.cgi which has multiple functions. One of those functions is to check to see if the parameter "verify" has been sent. It may not necessarily be sent because other forms use the script as well and won't have that hidden tag.

Thanks.

Replies are listed 'Best First'.
•Re: Checking for the existence of a passed parameter.
by merlyn (Sage) on Nov 11, 2003 at 19:04 UTC
      That's somewhat hard to read IMO. It hides the fact that this really is a tristate switch. I'd probably check for not-definedness separately.
      my $verify = param("verify"); if (not defined $verify) { # wasn't even there # ... # return, exit, last, or whatever here } if($verify eq 'yes') { # ... } else { # ... }
      This is Perl, let's not be writing Pascal code. *g*

      Makeshifts last the longest.

        But your code generates warnings when comparing $verify to 'yes' if it's also undef, unless you're absolutely sure that your code doesn't fall out the bottom. I'd write that like this, if you wanna flatten the nesting:
        if (not defined $foo) { ... undef ... } elsif ($foo eq 'yes') { ... yes ... } else { ... everything else ... }

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: Checking for the existence of a passed parameter.
by Zed_Lopez (Chaplain) on Nov 11, 2003 at 21:20 UTC
    This is overkill if that's the only test you need, but if there are others, consider Data::FormValidator.
Re: Checking for the existence of a passed parameter.
by injunjoel (Priest) on Nov 11, 2003 at 20:57 UTC
    Greetings all,
    Here is how I do it.
    use strict;#Always use CGI; my $q = new CGI; my $verify = (defined $q->param('verify'))?$q->param('verify'):0; if($verify){ ...do cool stuff here with $verify; }else{ ...$verify was not sent. }
    JAPH's perl hack.
    -injunjoel