gpt4 book ai didi

perl - "no autovivication"杂注在 Perl 中使用 grep 失败

转载 作者:行者123 更新时间:2023-12-03 16:53:05 24 4
gpt4 key购买 nike

我正在尝试使用以下模块关闭自动恢复:https://metacpan.org/pod/autovivification但 grep 失败了:

#!/usr/bin/env perl

use strict;
use warnings FATAL => 'all';
use feature 'say';
use autodie ':all';
use DDP;
no autovivification;

my %h = (
'a' => 1,
'b' => 2,
);
p %h; # pretty print the original hash
my @fake_keys = ('x', 'y', 'z');
if (grep {defined} @h{@fake_keys}) {
say "found fake keys";
}
p %h; # show that 'x', 'y', & 'z' are added as undef keys
如何关闭 grep 的自动激活?

最佳答案

这不是 grep但是导致这种情况的哈希片中的值的别名。
这具有相同的效果:

for (@h{@fake_keys}){
my $t = $_;
}
您可以通过复制切片来解决它:
use strict;
use warnings FATAL => 'all';
use feature 'say';
use autodie ':all';
use DDP;

my %h = (
'a' => 1,
'b' => 2,
);
p %h; # pretty print the original hash
my @fake_keys = ('x', 'y', 'z');
if (grep {defined} @{[@h{@fake_keys}]}) {
say "found fake keys";
}
p %h; # show that 'x', 'y', & 'z' are added as undef keys
或者访问 grep block 中的每个值,例如 shown池上.

关于perl - "no autovivication"杂注在 Perl 中使用 grep 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66156905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com