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 ''; }