문제

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.

도움이 되었습니까?

해결책

The Perl boolean operators like &&, ||, and, or don't return a boolean value, they return the value of one of their arguments:

say 2 && 3;

outputs 3.

You can force it to a boolean with the double negation trick:

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

outputs 1.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top