any pitfalls or better ways of doing this?
Yes, two of them. First, this code won't run under strict, and second, it allows malformed input to set any global variable it wants, which could do unforseen things with your script. You can solve both of these problems in either of two ways. The first is to use a single hash, instead of many globals. This is what I do:
foreach my $field ( @params ) { $input{$field} = param( $field ); }
This requires the rest of your script to use the hash values (like $input{foo} instead of just $foo), but it's straightforward and works. The other way is to expressly list what variables the user is permitted to set, and ignore any others. In order to get it to run under strict, you also have to add a line to allow the symbolic references:
foreach my $field qw(foo bar baz quux wibble) { no strict refs; $$field = param( $field ); }
This still requires you to list all the variables, but you don't have to have a separate assignment line for each, and you don't have to change the rest of the script to use a hash.
$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
In reply to Re: More efficient reading of forms in CGI
by jonadab
in thread More efficient reading of forms in CGI
by bradcathey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |