Closed SanyaZZZ closed 6 years ago
I just ran into this trying to make a thing that can use either module. Is this a problem with YAML::XS::Dump or with YAML::Load?
$ perl -MYAML -e 'print Dump( { "a" => [ "b" ] } )' | perl -MYAML::XS -e 'local $/; my $x = <STDIN>; my $y = Load( $x ); print $y->{a}->[0],"\n";'
b
$ perl -MYAML::XS -e 'print Dump( { "a" => [ "b" ] } )' | perl -MYAML -e 'local $/; my $x = <STDIN>; my $y = Load( $x ); print $y->{a}->[0],"\n";'
YAML Error: Invalid element in map
Code: YAML_LOAD_ERR_BAD_MAP_ELEMENT
Line: 3
Document: 1
at /usr/local/share/perl/5.10.1/YAML/Loader.pm line 352
The problem is, the output produced by YAML::XS::Dump() can't be parsed by YAML::Load() because of no indention for the inner array. I think YAML::XS::Dump() needs to be fixed.
No. The problem is that YAML::Load is broken.
foo:
- bar
Is valid YAML, and is what almost every modern YAML dumper will (and should) produce.
When you have a block sequence within a mapping, the '- ' counts as indentation.
YAML.pm was made way before the YAML spec was finalized. It should probably not be used in production. There are plans to rewrite it, but fixing this bug is probably not going to happen until then.
Ingy: I'm not sure if you are right here. The specs say: "Each node must be indented further than its parent node" http://yaml.org/spec/1.2/spec.html#6.1
The samples in the spec also use this indentation for seq elements. I would rather blame libyaml dumper, because it generates unparsable YAML. e.g. by the Perl PP YAML parser. But note that all other perl YAML readers can handle the unindented seq.
Should I fix libyaml dumper or YAML Loader?
Fixed with the option IndentlessMap in https://github.com/ingydotnet/yaml-libyaml-pm/pull/43
@rurban Please see
http://yaml.org/spec/1.2/spec.html#id2799181 8.2.3 Block Nodes
[201] seq-spaces(n,c) ::= c = block-out ⇒ n-1
c = block-in ⇒ n
http://yaml.org/spec/1.1/#id931893 10.1.2 Block sequences
[197] seq-spaces(n,c) ::= c = block-out ⇒ n-1
c = block-in ⇒ n
Both YAML 1.1 and 1.2 allow zero indented sequences. Like @ingydotnet said, YAML.pm is broken here. Pull requests for YAML.pm welcome.
In general, allowing to configure indendation and other stuff in YAML::XS would be desirable, but I don't like an IndentlessMap option.
The samples in the spec also use this indentation for seq elements.
Yes, examples use zero indented sequences. So?
Closing...
Released YAML.pm 1.25_001 which supports zero indented block sequences
There is no indent for an array in YAML::XS results, and this dump can't be loaded by YAML::Load(). Versions used: YAML v0.97, YAML::XS v0.52, perl v5.16.3 and 5.18.2