Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by Improv (Pilgrim)
on Apr 03, 2003 at 17:41 UTC ( [id://247821]=note: print w/replies, xml ) Need Help??


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

Try this:
my @varnames = qw{var1 var2 var3}; foreach my $var (@varnames) { if(${$var} eq "") {&complain}; }
It's kind of ugly, but it might be the best solution..

Replies are listed 'Best First'.
Re2: Validating the contents of scalars using an array based list
by dragonchild (Archbishop) on Apr 03, 2003 at 18:06 UTC
    It's kind of ugly, but it might be the best solution..

    It is ugly and it most definitely is not the best solution.

    The correct solution involves using a hash and iterating through that hash. One way of doing this could be:

    my @varnames = qw( Var1 Var2 Var3 ); my %params; @params{@varnames} = map { $cgi->param($_) } @varnames; if (grep { !defined $params{$_} || $params{$_} eq '' } @varnames) { &complain; } # Now, at this point, you have read in all your parameters # and verified that each is defined as well as not the # empty string. # Do other stuff here.
    This also illustrates a very important concept in application development - separating the things that can change from the things that won't ... aka Encapsulation. We are separating out the logic from the things being logic'ed. This is especially important for you because you don't know which varnames you want to apply this logic to.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Re: Validating the contents of scalars using an array based list
by chromatic (Archbishop) on Apr 04, 2003 at 03:44 UTC

    I like this a bit more than a hash, but why not use real references?

    foreach my $check ( \( $var1, $var2, $var3 ) ) { complain() if $$check eq ''; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-24 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found