Subject: | Load(Dump({"hi, world" => 1})) fails |
When dumping an otherwise simple hash key that contains a comma, YAML 0.35 and 0.49_01 both fail to quote the key. The parsers in both fail in different ways:
0.35:
--- !perl/YAML::Error
code: YAML_LOAD_ERR_BAD_MAP_ELEMENT
msg: Invalid element in map
line: 2
document: 1
...
0.49_01:
Can't use string ("") as a HASH ref while "strict refs" in use at YAML/Parser.pm line 48, <DATA> line 5.
I have attached a patch for 0.35 to make is_valid_implicit() return false if the scalar contains a comma. This causes strings with commas to be single quoted, which makes the code in the subject work as expected.
It's probably worth noting that I suspect the bug to be with the parser, but the parser in 0.35 is a little obtuse for me to want to dig in to it. :) The python parser syck handles the non-quoted form correctly, interpreting "---\nhi, world: 1\n" as {'hi, world': 1}. It also handles the quoted form correctly.
*** YAML-0.35/YAML.pm Tue Jun 25 15:02:09 2002
--- YAML-0.35-comma/YAML.pm Thu Feb 12 09:42:44 2004
***************
*** 604,610 ****
return 1 if $_[0] =~ /^-?\d+e[+-]\d+$/; # !float
# XXX - Detect date objects someday (or not)
return 0 if $_[0] =~ /$ESCAPE_CHAR/;
! return 0 if $_[0] =~ /(^\s|\:( |$)|\#( |$)|\s$)/;
return 1 if $_[0] =~ /^\w/; # !str
return 0;
}
--- 604,610 ----
return 1 if $_[0] =~ /^-?\d+e[+-]\d+$/; # !float
# XXX - Detect date objects someday (or not)
return 0 if $_[0] =~ /$ESCAPE_CHAR/;
! return 0 if $_[0] =~ /(^\s|\:( |$)|\#( |$)|\s$|\,)/;
return 1 if $_[0] =~ /^\w/; # !str
return 0;
}