我如何在Perl中使用`lock_hash_recurse`? [英] How do I use `lock_hash_recurse` in Perl?

查看:138
本文介绍了我如何在Perl中使用`lock_hash_recurse`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续讨论这里,我对 lock_hash_recurse 存在一些麻烦,如下图所示:

 #!/ usr / bin / perl 

use strict;
使用警告;
使用Data :: Dumper;
使用Hash :: Util qw(lock_keys);

my $ hashref = {A => 1,B => {CC => 22,DD => 33}};

lock_keys(%{$ hashref}); #这是好
Hash :: Util :: lock_hash_recurse(%{$ hashref}); #失败:在/usr/lib/perl/5.10/Hash/Util.pm第153行的字符串eq中使用未初始化的值。

据我所知, reftype 返回 undef ...是 lock_hash_recurse 中的一个错误(也许这就是为什么它不被导出的原因......)

解决方案

它是 Hash :: Util 中的一个错误。该代码表示​​:

$ $ p $ code> sub lock_hashref_recurse {
my $ hash = shift;

lock_ref_keys($ hash);
foreach my $ value(values%$ hash){
if(reftype($ value); eq'HASH'){
lock_hashref_recurse($ value);
}
内部:: SvREADONLY($ value,1);
}
return $ hash
}

但应该是:

  sub lock_hashref_recurse {
my $ hash = shift;

lock_ref_keys($ hash);
foreach my $ value(values%$ hash){
my $ type = reftype($ value);
if(defined $ type and $ type eq'HASH'){
lock_hashref_recurse($ value);
}
内部:: SvREADONLY($ value,1);
}
return $ hash
}

问题是 Scalar :: Util :: reftype 返回 undef ,不是空字符串。补丁已发送到p5p。它看起来不像 Hash :: Util 是一个双核(CPAN)核心模块,所以你必须升级到Perl 5版本固定。我建议你自己修补代码或编写你自己的版本。



如果你编写自己的版本,不要使用Internals :: SvREADONLY(用户级别的东西不应该使用内部元素包中的内容)。使用 Readonly :: XS 模块。

In continue to the discussion here, I'm havind some trouble with lock_hash_recurse as illustrated below:

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;
use Hash::Util qw (lock_keys);

my $hashref = {A=>1, B=>{CC=>22, DD=>33}};

lock_keys(%{$hashref}); # this is OK
Hash::Util::lock_hash_recurse(%{$hashref}); # this fails: "Use of uninitialized value in string eq at /usr/lib/perl/5.10/Hash/Util.pm line 153."

From what I can tell, reftype returns undef... is that a bug in lock_hash_recurse (maybe that's why it isn't exported?...)

解决方案

It is a bug in Hash::Util. The code says:

sub lock_hashref_recurse {
    my $hash = shift;

    lock_ref_keys($hash);
    foreach my $value (values %$hash) {
        if (reftype($value); eq 'HASH') {
            lock_hashref_recurse($value);
        }
        Internals::SvREADONLY($value,1);
    }
    return $hash
}

but should be:

sub lock_hashref_recurse {
    my $hash = shift;

    lock_ref_keys($hash);
    foreach my $value (values %$hash) {
        my $type = reftype($value);
        if (defined $type and $type eq 'HASH') {
            lock_hashref_recurse($value);
        }
        Internals::SvREADONLY($value,1);
    }
    return $hash
}

The problem is that Scalar::Util::reftype returns undef, not an empty string. A patch has been sent to p5p. It doesn't look like Hash::Util is a dual-life (in core and CPAN) module, so you would have to upgrade to a version of Perl 5 with it fixed. I would suggest either patching the code yourself or writing your own version.

If you write your own version, do not use Internals::SvREADONLY (user level stuff shouldn't use the stuff in the Internals package). Use the Readonly::XS module instead.

这篇关于我如何在Perl中使用`lock_hash_recurse`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆