Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Validating the contents of scalars using an array based list

by TGI (Parson)
on Apr 04, 2003 at 05:24 UTC ( [id://247963]=note: print w/replies, xml ) Need Help??


in reply to Validating the contents of scalars using an array based list

Try Params::Validate for all your parameter validation needs.

You can do something like this:

my $nonempty = sub { shift() ne '' }; my $check_params = { client1 => sub { validate( @_, { var1 => { type => SCALAR, # a scalar ... callbacks => { # and is non-empty 'not empty' => $nonempty, }, }, var2 => { type => SCALAR, # a scalar ... callbacks => { # and is non-empty 'not empty' => $nonempty, }, }, } ); }, client2 => sub { # more of the same }, }; my %vars; my @varnames = $q->param; @vars{@varnames} = map { $q->param($_) } @varnames; $check_params->{$client}->(%vars);

With this approach you can check for type, regex matching or require that an arbitrary subroutine(s) return true. You may want to consider generating the hash of functions, instead of having it hard coded.

Params::Validate is very nice.


TGI says moo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://247963]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found