in reply to Re: Tidy up conditions
in thread Tidy up conditions

Thanks heaps for that, Martin!  Awesome answer!  Concise, yet...functional.

I don't understand your concern about boolean false values, though. Could you give me an example which would make your code fail, please?

Thanks again.
Tel2

Replies are listed 'Best First'.
Re^3: Tidy up conditions
by SuicideJunkie (Vicar) on Mar 19, 2015 at 20:09 UTC
    my %hash = (0=>'that was false', ''=>'oops false too', 42=>'So very true');

    The first two keys there evaluate to false, and would cause a problem with the code.

      OK - thanks SJ,

      I had thought that when Martin said "boolean false values", he literally meant hash values, as opposed to hash keys, which is why I questioned it, but I guess he meant values in the more general sense (i.e. keys in this case).

        Actually, I did mean values and not hash keys. Here is an example of how boolean false values would defeat our intent to short-cut at the first match:
        my %access = ('foo' => 0, 'bar' => 123); print $access{'foo'} || $access{'bar'}; # prints 123
        The key 'foo' exists but the zero score makes perl evaluate the second operand, too. With perl version 5.10.0 and up, the defined-or operator can help, as it short-cuts only on undef:
        my %access = ('foo' => 0, 'bar' => 123); print $access{'foo'} // $access{'bar'}; # prints 0
        Older perls don't have this operator, however.