Subject: | Compatibility with Ruby's yaml module |
Date: | Mon, 08 Dec 2008 12:30:13 +0000 |
To: | bug-YAML-Tiny [...] rt.cpan.org |
From: | Paul Clifford <paul.clifford [...] bbc.co.uk> |
Ruby's yaml module uses "- " as the indent for list items:
$ ruby -ryaml -e 'print YAML.dump({"a" => [42], "b" => [42]})'
---
a:
- 42
b:
- 42
YAML::Tiny can't parse this because it assumes that the indent level is
only determined by the number of leading spaces:
$ perl -MYAML::Tiny -e 'YAML::Tiny::Load("--- \na: \n- 42\nb: \n- 42\n")'
YAML::Tiny does not support the line 'b: ' at
/usr/lib/perl5/vendor_perl/5.10.0/YAML/Tiny.pm line 236.
On the other hand, YAML::Syck does recognise it:
$ perl -MYAML::Syck -e 'Load("--- \na: \n- 42\nb: \n- 42\n")'
(no output)
Here's a patch to YAML-Tiny-1.34_01 which fixes the problem. There's a
similar bug in the YAML library so the new test uses noyamlpm:
http://rt.cpan.org/Public/Bug/Display.html?id=25945
http://www.bbc.co.uk/
This e-mail (and any attachments) is confidential and may contain personal views which are not the views of the BBC unless specifically stated.
If you have received it in error, please delete it from your system.
Do not use, copy or disclose the information in any way nor act in reliance on it and notify the sender immediately.
Please note that the BBC monitors e-mails sent or received.
Further communication will signify your consent to this.
diff --git a/lib/YAML/Tiny.pm b/lib/YAML/Tiny.pm
index df903c8..e03403f 100644
--- a/lib/YAML/Tiny.pm
+++ b/lib/YAML/Tiny.pm
@@ -190,8 +190,8 @@ sub _read_array {
}
# Check the indent level
- $lines->[0] =~ /^(\s*)/;
- if ( length($1) < $indent->[-1] ) {
+ $lines->[0] =~ /^(\s*)(-?)/;
+ if ( length($1) < $indent->[-1] or $2 ne '-' ) {
return 1;
} elsif ( length($1) > $indent->[-1] ) {
die "Hash line over-indented";
diff --git a/t/02_basic.t b/t/02_basic.t
index 994be4e..03675a7 100644
--- a/t/02_basic.t
+++ b/t/02_basic.t
@@ -10,7 +10,7 @@ BEGIN {
use File::Spec::Functions ':ALL';
use t::lib::Test;
-use Test::More tests(30);
+use Test::More tests(31);
use YAML::Tiny;
@@ -129,6 +129,20 @@ END_YAML
'array_in_hash',
);
+# Array inside a hash, with no leading spaces
+yaml_ok(
+ <<'END_YAML',
+---
+a:
+- 42
+b:
+- 42
+END_YAML
+ [ { a => [ 42 ], b => [ 42 ] } ],
+ 'array_in_hash',
+ noyamlpm => 1,
+);
+
# Simple hash inside a hash with an undef
yaml_ok(
<<'END_YAML',