ingydotnet / yaml-libyaml-pm

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

!! perl/code tag emitted in Dump, not Loading back in in Load #28

Closed jasonjayr closed 5 years ago

jasonjayr commented 9 years ago
use YAML::XS;
use Data::Dumper;
$YAML::XS::DumpCode = 1;
$YAML::XS::LoadCode = 1;
my $ds = { hook => sub { 'silliness' }, name => 'jason' };
my $yaml = YAML::XS::Dump($ds);
print "YAML:\n $yaml\n";
# $yaml =~ s/!!perl\/code//; <-- super-hacky workaround.
my $restored = YAML::XS::Load($yaml);
print pp($restored);

The Dump function, whether or not DumpCode is set, will emit tagged scalars when it encounters a code ref. Trying to Load that yaml back in, will trigger this error:

YAML::XS Error: bad tag found for scalar: 'tag:yaml.org,2002:perl/code' at test.pl line 9.

YAML::XS::LoadCode seems to not be referenced anywhere except in the documentation and does nothing useful.

Would it be legit to patch DumpCode = 0 to skip over code refs (or perhaps, if set to -1 to maintain current behavior)? To get code to load back in properly, all that's needed is an eval in the .xs?

perlpunk commented 6 years ago

Yes, I think omitting the tag when $DumpCode is 0 would be a valid behaviour. On the other hand, that doesn't match what YAML.pm and YAML::Syck do. To fix this, we could just ignore the code tag in Load for now instead of dying. Implementing $LoadCode in XS is not trivial, but should be doable by looking at YAML::Syck or Storable.

perlpunk commented 6 years ago

I just released YAML::XS 0.74_001 with support for $YAML::XS::LoadCode. I did it via a call to a perl function as this was the easiest thing to do.

perlpunk commented 5 years ago

Released 0.75