我如何能布尔条件结果分配到Perl中的标量变量? [英] How can I assign a boolean condition result to a scalar variable in perl?

查看:127
本文介绍了我如何能布尔条件结果分配到Perl中的标量变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做以下,但它不能正常工作:

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

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

这是一个布尔值赋给一个标量能够接受的方式?
我的code为行为不端以奇怪的方式,因为它是的,我相信这是因为这个任务的。我已验证了对所有这些键的存在的单独的值和被设置为一个值

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。对不起,作为一个菜鸟!我用Google搜索〜10分钟,找不到任何地方的答案。

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

推荐答案

Perl的布尔运算符如&放大器;&安培; || 不返回布尔值,它们将返回一个值他们的论点:

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;

输出 3

您可以用双重否定招迫使它一个布尔值:

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

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

输出 1

这篇关于我如何能布尔条件结果分配到Perl中的标量变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆