# Predeclare which parameters are needed and also specify # which are expected to contain lists. All others are # expected to contain scalars. use constant EXPECTED_PARAMETERS => { sort_preference => '', signature => '', display_fields => 'ARRAY' }; # Get the CGI object my $q = CGI->new; # Get the parameters and store in a hash reference my $params = get_parameters( $q, EXPECTED_PARAMETERS ); # Do whatever the script does ..... sub get_parameters { # The first parameter is the CGI object. # The second parameter is an hash reference # to the list of expected parameters. The values # are examined for the string 'ARRAY' and those # are then retrieved into array references. my $q = shift; my $expected = shift; # Get a list of all the parameter names in a hash # This allows me to do exists() tests for parameters my %params; my @params{ $q -> params } = (); # Now go get each expected parameter my %extracted; while (my ($param_name, $param_type) = each %$expected) { # warn of missing parameters unless (exists $params{$param_name}) { cluck "Missing CGI parameter $param_name"; next; } # Copy the value over. This does a bit of fancy # footwork by extracting 'ARRAY' parameters into # arrays and all others into a plain scalar. It is # not sufficient to simply copy over values since # the behaviour for CGI's param() function changes # depending on the value of wantarray(). $extracted{$param_name} = $param_type eq 'ARRAY' ? [ $q -> param($param_name) ] : $q -> param($param_name); # Return the expected parameter list as a # hash reference. return \ %extracted }