How can I assign a boolean condition result to a scalar variable in perl?

后端 未结 1 341
自闭症患者
自闭症患者 2021-01-14 14:38

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

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


        
1条回答
  •  南笙
    南笙 (楼主)
    2021-01-14 15:20

    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.

    0 讨论(0)
提交回复
热议问题