Hash designed for threads, to be dumped in YAML format

Viewed 105

Trying to dump the nested hash map with shared references, in YAML file. Perl code is here. The hash is actually made thread friendly,

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;

and in the end, I am trying to dump out the flow, it is showing the reference as mentioned below

% ./ref-of-refs.pl ; cat test.yaml
---
anotherRef: HASH(0x21897a0)

Is there any option to dump the full hash into YAML format in test.yaml file. ?

1 Answers

My guess is that the XS magic that 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 instead:

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);

Output:

---
anotherRef:
  test: {}
Related