in reply to More efficient reading of forms in CGI
Your solution might be fast, but it also allows anyone to clobber the value of any variable in the package's namespace. Depending on how you code, this may or may not be a problem, but me thinks it's better to avoid the problem entirely.
Instead, explicitly put all the names of the fields you allow in an array. My arg-parsing routines usually look something like this:
{ my @FIELDS = qw( foo bar baz ); sub set_params { use CGI qw(:standard); map { $_ => param($_) || '' } @FIELDS; } } # Other subroutines here { my %params = set_params(); # Call other subroutines here }
It's much safer, and a change in the fields only means a change in the @FIELDS array. I use a similar form for object constructors:
{ my @FIELDS = qw( foo bar baz ); sub new { my $class = shift; my $in = shift || { }; # Passed in a hashref my %self = map { $_ => $in->{$_} || '' } @FIELDS; bless \%self, $class; } }
----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
|
|---|