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

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

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';

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://840423]
[1nickt]: Ya gotta love the google. If you set accept=".txt" in a form input field where type="file", Chrome will allow you to select local files with extensions like .pl, .pm, .yaml, .ini etc. Nice!

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2017-07-22 18:24 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (340 votes). Check out past polls.