Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#1 Punctuation in hash keys messes up YAML

v1.0 (example)
open
nobody
None
5
2003-09-26
2003-09-26
Anonymous
No

A hash with a comma as part of a hash key, produced
like this:

use strict;
use YAML;

my $tradeobj = bless( {
'Trade Route (Fort Apache, Blue)' => 1
}, 'tradeObject' );

my $dump = Dump($tradeobj);
print $dump;

Produces a YAML output like this:

--- #YAML:1.0 !perl/tradeObject
Trade Route Fort Apache, Blue: 1

The comma causes YAML to be unable to parse what it wrote:

use strict;
use YAML qw(LoadFile);

my $trade = LoadFile("trade.yaml");
print "Did it\n" if defined($trade);

The result is:

--- !perl/YAML::Error
code: YAML_LOAD_ERR_BAD_MAP_ELEMENT
msg: Invalid element in map
line: 2
document: 1
...
at tradein.pl line 4

Simple quotes around the problem key will eliminate the
error. so it seems to me that YAML should check for odd
characters in hash keys and supply the quotes if needed.

Thanks for your attention.

Discussion

  • Clark C. Evans
    Clark C. Evans
    2003-10-18

    Logged In: YES
    user_id=5162

    Quite right; however, this parser was written when the
    plain values allowed for keys were more strict. You may
    want to consider using Syck, it now has a Perl binding.