根据这个问题的答案,在Perl 5中合并两个哈希值可以通过以下方式完成:
%newHash = (%hash1, %hash2); %hash1 = %newHash;
Run Code Online (Sandbox Code Playgroud)
要么:
@hash1{keys %hash2} = values %hash2;
Run Code Online (Sandbox Code Playgroud)
另一方面,Perl 5.18将引入每进程哈希随机化.
第二种方法在Perl 5.18中使用是否仍然正确?
我使用Reval公司从Perl的安全模块,我想阻止它产生警告如果被eval'ed字符串不能被解析(实际上,我想阻止它产生任何警告的话).
例如,以下代码:
use strict; use warnings;
use Safe;
use feature qw/say/;
my $cft = Safe->new;
my $x = $cft->reval(') 1' );
my $y = $cft->reval('2' );
say "x: $x";
say "y: $y";
Run Code Online (Sandbox Code Playgroud)
结果是:
Number found where operator expected at (eval 5) line 1, near ") 1"
(Missing operator before 1?)
Use of uninitialized value $x in concatenation (.) or string at ./test line 12.
x:
y: 2
Run Code Online (Sandbox Code Playgroud)
我想要实现的是$ x = undef和$ y = 2,没有警告.我试图提出"没有警告;" 在新范围内,但它对reval中产生的警告没有影响(尽管如@DavidO指出的那样,它会使'未初始化值'警告静音):
use …Run Code Online (Sandbox Code Playgroud)