Reputation: 38237
To check if a request parameter has a value (http://example.com?my_param=value) you can use
query->param('my_param')
This seems to be flaky if the no_value form is used: http://example.com?my_param .
Is there a way to check simply the existence of parameters in CGI.pm ?
Upvotes: 1
Views: 561
Reputation: 2341
You can check whether the parameter is present with defined. the following code
#!/usr/bin/env perl
use strict;
use warnings;
use CGI;
my $cgi = CGI->new();
print $cgi->header;
my $defined = defined $cgi->param('tt');
my $val = $cgi->param('tt');
print <<END;
<!doctype html>
<html> HTML Goes Here
<h1>tt: "$defined" "$val"</h1>
</html>
END
Will generate the output:
<!doctype html>
<html> HTML Goes Here
<h1>tt: "1" ""</h1>
</html>
when the URI is http:///cgi-bin/t.pl?someother=something&tt
Unfortunately this doesn't work when it is the only parameter.
So to be sure, you have to test:
my $defined = defined $cgi->param('tt') || $ENV{QUERY_STRING} eq 'tt';
Upvotes: 3
Reputation: 54333
This seems to depend on the browser implementation. According to this Perlmonks thread from 2001, empty parameters might not be returned. But if they are, the key will be there, but contain an empty string q{}
. If the argument wasn't present, it will be undef
, so you can check with defined
.
Upvotes: 2