加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

我应该在Perl中使用$hash {“string”}还是$hash {string}?

发布时间:2020-12-15 22:04:24 所属栏目:大数据 来源:网络整理
导读:在Perl中,哪些是“更好”的风格? $hash{"string"} or $hash{string}? 无论如何,它们的功能是否相同? 解决方法 从 perldata perldoc: In fact,an identifier within such curlies is forced to be a string,as is any simple identifier within a hash sub
在Perl中,哪些是“更好”的风格?
$hash{"string"} or $hash{string}?

无论如何,它们的功能是否相同?

解决方法

从 perldata perldoc:

In fact,an identifier within such curlies is forced to be a string,as is any simple identifier within a hash subscript. Neither need quoting. Our earlier example,$days{'Feb'} can be written as $days{Feb} and the quotes will be assumed automatically. But anything more complicated in the subscript will be interpreted as an expression. This means for example that $version{2.0}++ is equivalent to $version{2}++,not to $version{'2.0'}++

所以是基本相同的.不过要小心:

sub is_sub { 'Yep!' }

my %hash;
$hash{ is_sub   } = 'Nope';
$hash{ is_sub() } = 'it is_sub!!';

say Dumper %hash;

将会呈现:

$VAR1 = { 'is_sub' => 'Nope','Yep!' => 'it is_sub!!' };

我喜欢的是这个裸字…但是记住这些()或前一个(见jrockway的评论和答案),如果你打电话给sub

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读