ingydotnet / yaml-libyaml-pm

Perl Binding to libyaml
http://search.cpan.org/dist/YAML-LibYAML/
33 stars 37 forks source link

Dump() does not work with threads::shared hashes. #100

Closed hakonhagland closed 3 years ago

hakonhagland commented 3 years ago
use strict;
use warnings;
use YAML::XS;
use threads;
use threads::shared;

my %hash : shared;
my %foo : shared;
$foo{bar} = 1;
$hash{foo} = \%foo;
print Dump(\%hash);

Output:

---
foo: HASH(0x55ec099e22c0)

Expected output:

---
foo:
  bar: 1

See also this question on stackoverflow.

perlpunk commented 3 years ago

Thanks, patches welcome :)

perlpunk commented 3 years ago

btw, the output for YAML::Syck::Dump is:

--- {}
perlpunk commented 3 years ago

I think a call to SvGETMAGIC in dump_node fixed it. It seems to also fix #7

perlpunk commented 3 years ago

I think I still need help because the detection of references does not work. I added a SvGETMAGIC call at the top of dump_prewalk https://github.com/ingydotnet/yaml-libyaml-pm/blob/master/LibYAML/perl_libyaml.c#L827 But this code:

my %hash : shared;
my %foo : shared;
$foo{bar} = 1;
$hash{foo} = \%foo;
$hash{x} = \%foo;
print Dump(\%hash);

returns

foo:
  bar: 1
x:
  bar: 1

instead of

foo: &1
  bar: 1
x: *1
perlpunk commented 3 years ago

I pushed what I have so far to the tied branch: https://github.com/ingydotnet/yaml-libyaml-pm/tree/tied

perlpunk commented 3 years ago

Hm, the fact that the reference isn't recognized seems to be normal, since Data::Dumper also outputs:

$hash = {
          'foo' => {
                     'bar' => 1
                   },
          'x' => {
                   'bar' => 1
                 }
        };

and not

$hash = {
          'x' => {
                   'bar' => 1
                 },
          'foo' => $hash->{'x'}
        };
perlpunk commented 3 years ago

I think I fixed it: #101

perlpunk commented 3 years ago

Finally released v0.83!