3

I am doing the following, but it is not working properly:

    my $enabled = $hash && 
                  $hash->{'key'} && 
                  $hash->{'key'}->{'enabled'} && 
                  $hash->{'key'}->{'active'};

Is this an acceptable way to assign a boolean value to a scalar variable? My code is misbehaving in strange ways, as it is, and I believe it is because of this assignment. I have validated that the individual values exist for all of these keys and are set to a value.

P.S. Sorry for being a noob! I googled for ~10 minutes and couldn't find the answer anywhere.

4

1 回答 1

7

Perl 布尔运算符,如&&, ||, and,or不返回布尔值,它们返回其参数之一的值:

say 2 && 3;

输出3

您可以使用双重否定技巧将其强制为布尔值:

say !!(2 && 3);
# or
say not not 2 && 3;

输出1

于 2013-06-26T19:52:44.917 回答