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

前端之家收集整理的这篇文章主要介绍了我应该在Perl中使用$hash {“string”}还是$hash {string}?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在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

猜你在找的Perl相关文章