perl - 删除有或没有删除的祝福散列成员

标签 perl hash taint

我在一些资料中看到了这行代码

( $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/

相关文章:

perl - 自动调用作为子程序引用的哈希值

json - Ballerina,使用来自 REST-API 的 Json 响应

linux-kernel - 看到一些内核污染消息后,printk 将不再工作

linux - git checkout --patch,但失败,无法在@INC 中找到 Git.pm

Perl while循环/读取文件

c - Text::JaroWinkler::strcmp95 的第三个参数是什么?

perl - 如何在不实际解包的情况下获取 Perl 中打包项目的数量?

java - HashMap 中可以发生多少次重新哈希

ruby-on-rails - 如何将新哈希插入到 ruby​​ 中的现有哈希中

security - 在 Haskell 中从 'Taint mode' 复制 'Fortify static checking tool'