in reply to Re^4: Passing flags between invocations of a cgi script
in thread Passing flags between invocations of a cgi script

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.)

Replies are listed 'Best First'.
Re^6: Passing flags between invocations of a cgi script
by chromatic (Archbishop) on May 18, 2010 at 16:18 UTC

    To replicate the defined-or operator, ancient versions of Perl require the far clumsier:

    my $foo = defined $q->param('foo') ? $q->param('foo') : 'Default Value';