尝试在 YAML 文件中转储具有共享引用的嵌套 HashMap 。 Perl 代码在这里。哈希实际上是线程友好的,
use v5.18;
use YAML::XS;
use threads;
use threads::shared;
use Data::Dumper;
my $href1 = shared_clone({
anotherRef => shared_clone({})
});
$href1->{anotherRef}{test} = &share({});
print Dumper $href1;
my $path = "./test.yaml";
open TAG_YAML, '>', $path;
print TAG_YAML Dump($href1);
close TAG_YAML;
1;
最后,我试图转储流,它显示了如下所述的引用
% ./ref-of-refs.pl ; cat test.yaml
---
anotherRef: HASH(0x21897a0)
是否有任何选项可以将完整哈希转储为 test.yaml 文件中的 YAML 格式。 ?
最佳答案
我的猜测是 XS 魔法 threads::shared使用混淆 YAML::XS::Dump()
(因为它还使用 XS 遍历 hashref)。我建议您尝试使用纯 Perl 模块 YAML相反:
use strict;
use warnings;
use YAML;
use threads;
use threads::shared;
my $href1 = shared_clone({
anotherRef => shared_clone({})
});
$href1->{anotherRef}{test} = shared_clone({});
print Dump($href1);
输出:
---
anotherRef:
test: {}
关于multithreading - 为线程设计的哈希,以 YAML 格式转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67033350/