在尝试减少一些默认哈希码的同时,我发现你可以添加到none来生成任何哈希代码或者生成你要添加的内容.这有什么特别的原因吗?这会改变不同的架构还是我可以依靠这种能力?
DB<1> print none + 1
DB<2> print 1 + none
1
Run Code Online (Sandbox Code Playgroud)
而对于那些好奇的人来说,这就是我使用它的方式
foreach (@someArray) {
unless ($someHash{$_}++) {
$someHash{$_} = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
作为减少
foreach (@someArray) {
if (exists $someHash{$_}) {
$someHash{$_}++;
} else {
$someHash{$_} = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
你没有做你认为自己在做的事情.这两个陈述:
print none + 1
print 1 + none
Run Code Online (Sandbox Code Playgroud)
并不像你想象的那么简单.因为你关闭了警告,你不知道他们做了什么.让我们在命令提示符下尝试它们,并打开警告(-w
切换):
$ perl -lwe'print none + 1'
Unquoted string "none" may clash with future reserved word at -e line 1.
Name "main::none" used only once: possible typo at -e line 1.
print() on unopened filehandle none at -e line 1.
$ perl -lwe'print 1 + none'
Unquoted string "none" may clash with future reserved word at -e line 1.
Argument "none" isn't numeric in addition (+) at -e line 1.
1
Run Code Online (Sandbox Code Playgroud)
在第一种情况下,none
它是一个裸字,被解释为文件句柄,并且print语句失败,因为我们从未打开具有该名称的文件句柄.在第二种情况下,裸字none
被解释为一个字符串,由加法运算符转换为数字+
,该数字将为零0
.
您可以通过为第一种情况提供特定的文件句柄来进一步澄清这一点:
$ perl -lwe'print STDOUT none + 1'
Unquoted string "none" may clash with future reserved word at -e line 1.
Argument "none" isn't numeric in addition (+) at -e line 1.
1
Run Code Online (Sandbox Code Playgroud)
这表明none + 1
和之间没有真正的区别1 + none
.