Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: checking values of variables

by toolic (Bishop)
on Aug 12, 2013 at 14:25 UTC ( #1049130=note: print w/replies, xml ) Need Help??

in reply to checking values of variables

Instead of declaring 50 variables, you could declare a single hash.
my %params; # some code here which may or may not add keys to %params print "help\n" unless %params;

Replies are listed 'Best First'.
Re^2: checking values of variables
by Laurent_R (Canon) on Aug 12, 2013 at 15:39 UTC

    I guess that would not work, because the OP apparently wants to trigger the help function if any of the variables is absent, not if they are all missing.

    So using a hash, it would have to be something like this:

    help() if 22 > scalar values %params; # scalar unnecessary, just to make the context clearer

    But that would not be very robust, for example if the %params may have some elements other that the list of mandatory parameters.

    So an approach more reliable than just counting the hash values would be to test each and every single mandatory param:

    for (qw / server database user password ... initversion /) { help() and last unless (defined $params{$_} and $params{$_}); }

    The exact test on the %params values would depend on how the hash is initialized.

Re^2: checking values of variables
by fishmonger (Chaplain) on Aug 12, 2013 at 16:03 UTC
    print "help\n" unless %params;

    That does not do what the OP's if conditional does. The OP want's to execute help() if any one of the vars is empty. Your unless test requires ALL vars to be empty.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1049130]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2018-06-22 17:36 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.