为线程设计的哈希,以 YAML 格式转储

Hash designed for threads, to be dumped in YAML format

尝试在 YAML 文件中转储具有共享引用的嵌套哈希映射。 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 格式。 ?

我的猜测是 threads::shared uses confuses YAML::XS::Dump() (since it also uses XS to traverse the hashref). I would suggest you try the pure Perl module YAML 代替的 XS 魔法:

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: {}