For simple scripts, I might use something like:
my $foo = $q->param('foo') // 'Default Value';
This gives precedence to the CGI form data, but if no form data for that value exists, assign a default (which in this case is 'Default Value').
The // operator requires Perl 5.10. Older versions of Perl will need the clumsier:
my $foo = defined $q->param('foo') ? $q->param('foo') : 'Default Value';
For non-trivial scripts, I usually put this in a loop, with a predetermined hash of defaults and allowed variable names. There's a good chance you'll need some additional logic as well, to validate user input and protect against invalid form submissions.
Edit: Forgot `defined' on 2nd example, which was sort of the whole point of that example. :-) (Thanks chromatic.)
|