http://qs1969.pair.com?node_id=1049133


in reply to checking values of variables

The code that have posted is rather cumbersome, but it does have one significant advantage: it will be much easier to understand a year or so down the road when a small change has to be made - especially if someone else has to make the change. So while there are more elegant ways of coding this, the code you have is about as clear an expression of your desired behavior as you will find.

Replies are listed 'Best First'.
Re^2: checking values of variables
by AnomalousMonk (Archbishop) on Aug 12, 2013 at 16:01 UTC
    ... about as clear an expression of your desired behavior as you will find.

    I would have thought that something like the following would be much more clear. Note that List::MoreUtils::any has the same short-circuiting behavior as  || (logical-or).

    use List::MoreUtils qw(any); ... help() if any { $_ eq q{} } $server, $user, $database, $password, ... +;
      While your suggested code is a good solution; I was thinking along the lines of being able to match more than just an empty string for each case. So List::MoreUtils could be used to express the code this way:
      help() unless all { $_ } $server, $user, $database, ...
      or without List::MoreUtils:
      help() unless $server && $user && $database ..
      but I still think that the original code makes it very clear what is going on and provides future flexibility. I tend to believe that code golf and maintainablity are often at odds.
        I tend to believe that code golf and maintainablity are often at odds.

        As do I. In fact, I would go a bit further and say that code golf and maintainablity are almost always at odds. Clarity is immensely important for maintainable code and concision or terseness is a very important element of clarity, along with many others. (Of course, terseness is the only consideration in 'golfed' code!)

        So List::MoreUtils could be used to express the code this way:
        help() unless all { $_ } $server, $user, $database, ...
        or without List::MoreUtils:
        help() unless $server && $user && $database ..
        but I still think that the original code makes it very clear what is going on and provides future flexibility.  
        [emphasis added]

        The problem I see with either a solution like
            help() unless $server && $user && $database ..
        or like the OPed code is that it is inflexible: if it becomes necessary to validate for a different condition or set of conditions, you're stuck making a change in who-knows-how-many locations in a file. The advantage of a test like the one in
            help() unless all { $_ } $server, $user, $database, ...
        is that it is defined in one place: DRYSM.
        (BTW: Both of these solutions differ from the OPed code in that they reject a variable with a  '0' string or numeric value of 0.)

        In fact, if it was the case or if there was any likelihood that the group of variables involved in the OPed code or the condition against which they were being tested would occur again elsewhere in the code, I would, in the interests of maintainability, be strongly tempted to go for whole-hog generalization (even though some might criticize it as premature) with something like this:

        use List::MoreUtils qw(any); ... my ($server, $user, $database, $password, ... ); ... sub IMPORTANT_STUFF () { return $server, $user, $database, $password, +... ; } sub importantCriterionForImportantStuff () { return $_ ne ''; } ... help() unless all { importantCriterionForImportantStuff } IMPORTANT_ST +UFF;

        Or, still without List::MoreUtils:

        help() if grep {! $_} $server, $user, $database, ...;
Re^2: checking values of variables
by fionbarr (Friar) on Aug 12, 2013 at 15:18 UTC
    thanks