in reply to Things every perl programmer should know?
My CGIs have a set_params() subroutine which returns a hash of all the paramters I want (I used to set global variables, but I kicked myself of that habit). Orginally, I did this one param at a time:
sub set_params { use CGI qw(:standard); my %params; $params{param1} = param('param1') || ''; $params{param2} = param('param2') || ''; $params{param3} = param('param3') || ''; # Dozen more lines of that return %params; }
Now I keep a constant global array named @FIELDS which contains a list of all the fields I want and put them together with map:
my @FIELDS = qw(param1 param2 param3 . . . ); sub get_params { use CGI qw(:standard); return map { $_ => param("$_") || '' } @FIELDS; }
You can remove the return in the above, but I kept it in this example for clarity.
----
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
|
---|