i have a code with many OR conditions like this (working fine) to check if one of the values is empty, then we throw an error message (all of them must be filled)
} elsif (
!$params{'account'}
|| !$params{'type'}
|| !$params{'sampledate'}
|| !$params{'lastpwchange'}
|| !$params{'system'}
|| !$params{'wspass'}
) {
print $cgi->header("text/plain"), "some mandatory parameters are missing";
}
i need to add many other variables in that condition, making the line a bit unreadable.
i was wondering if there was a more elegant way of specifying all of them, maybe using an array/list of some kind ?
thank you
Using handy module List::Util
, you can do something like:
use List::Util qw/any/;
...
elsif (any { !$params{$_} } qw/account type sampledate lastpwchange system wspass/) {
print $cgi->header("text/plain"), "some mandatory parameters are missing";
}
Please note that your expression does not exactly checks for emptiness; it actually performs a boolean check, so a parameter having value 0
would also fail the check. This might, or might not be what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With