in reply to Validating incoming CGI form data

I like to use a hash that links regular expressions and parameters. Like so:
use strict; use CGI; my $query=CGI->new(); my %laundry= ( 'name' => '(\w+)', 'number' => '(\d+)', 'choice' => ([ABC]), ); my %laundered_params; foreach my $param (keys %laundry) { die "Parameter $param not present\n" unless my $input=$cgi->param($p +aram); my $regexp=$laundry{$param}; $input =~ /$regexp/; die "Invalid input for $param\n" unless $laundered_params{$param}=$1 +; }
The above code is untested and assumes the use of CGI.

CU
Robartes-

Replies are listed 'Best First'.
Re^2: Validating incoming CGI form data (more concise)
by Aristotle (Chancellor) on May 02, 2003 at 14:44 UTC
    Or maybe like this:
    while(my ($param, $rx) = each %laundry) { local $_ = $cgi->param($param) or die "Parameter $param not present\n"; ($laundered_params{$param}) = /$rx/; or die "Invalid input for $param\n"; }
    Note that both snippets will behave in probably undesired ways if any of the parameters have multiple values.

    Makeshifts last the longest.