Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^5: Passing flags between invocations of a cgi script

by wanna_code_perl (Pilgrim)
on May 17, 2010 at 20:51 UTC ( #840423=note: print w/ replies, xml ) Need Help??


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


Comment on Re^5: Passing flags between invocations of a cgi script
Select or Download Code
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';

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://840423]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-08-20 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (113 votes), past polls