As your appropriate values might be 0 or 1, this is going to fail on a valid submission. Better to have:unless ($query->param('is_a_chicken')) # Fails on 0 { 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:unless (length $query->param('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: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"; }
Which would simplify as follows:sub provided { return defined($_[0]) && length ($_[0]); }
unless (provided ($query->param('is_a_chicken')) { print "You must choose an option.<BR>\n"; }
In reply to Re^2: check for undefined param name
by tadman
in thread check for undefined param name
by cameltrader
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |