我在一些资料中看到了这行代码
( $self->{arg} ) = ( ( delete $self->{arg} ) =~ /(.*)/s ) if ${^TAINT};
我理解无污染。我也知道 delete
我的问题是,在什么情况下需要或首选使用
delete
,而且使用更简单的还不够( $self->{arg} ) = ( ( $self->{arg} ) =~ /(.*)/s ) if ${^TAINT};
例如
#!/usr/bin/env perl -T
use 5.014;
use warnings;
package Some {
use Moose;
has 'arg' => (is => 'rw', isa => 'Str');
sub doit {
my $self = shift;
#( $self->{arg} ) = ( ( delete $self->{arg} ) =~ /(.*)/s ) if ${^TAINT};
( $self->{arg} ) = ( ( $self->{arg} ) =~ /(.*)/s ) if ${^TAINT};
}
};
my $some = Some->new( arg => 'some text' );
$some->doit();
say $some->arg;
最佳答案
使用普通哈希删除值并重新插入将产生与在适当位置修改它相同的结果。
commit没有提供关于他为什么删除它的任何信息,只是因为他复制了 Mason 1 的功能。但是如果您查看 HTML::Mason::Lexer 的来源,你会发现这个评论:
We need to untaint the component or else the regexes will fail to a Perl bug. The delete is important because we need to create an entirely new scalar, not just modify the existing one.
($current->{comp_source}) = (delete $current->{comp_source}) =~ /(.*)/s if taint_is_on;
所以这样做的原因是有一个新的标量,尽管他没有为他没有污染的另一个地方这样做:Mason::Interp ,所以我的猜测是一个早期的 Perl 错误,当 untainting 时。所以区别在于
delete
会给你一个新的标量,虽然这很少有实际应用。 (删除和插入当然也是一个较慢的操作。)use strict;
my $hash->{test} = 'test';
print \($hash->{test}),"\n";
( $hash->{test} ) = ( ( $hash->{test} ) =~ /(.*)/s );
print \($hash->{test}),"\n";
( $hash->{test} ) = ( ( delete $hash->{test} ) =~ /(.*)/s );
print \($hash->{test}),"\n";
给SCALAR(0x7f84d10047e8)
SCALAR(0x7f84d10047e8)
SCALAR(0x7f84d1029230)
关于perl - 删除有或没有删除的祝福散列成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33718328/