Skip to content

Commit

Permalink
fix error with hash keys beginning with '-'
Browse files Browse the repository at this point in the history
YAML::Tiny fails to parse a hash where the key beigns with
'-' (e.g. '-foo') with the error:
YAML::Tiny found illegal characters in plain scalar: 'family: 'Courier 10 Pitch'' at test-yaml.pl line 21.

Here's an example below, with a test program, the yaml file written by
the test program and the result of the script:

$ cat test-yaml.pl
use strict;
use warnings;
use YAML::Tiny;
use XXX;

my $data = {
          '-weight' => 'normal',
          '-overstrike' => 0,
          '-size' => -13,
          '-slant' => 'roman',
          '-underline' => 0,
          '-family' => 'Courier 10 Pitch'
        };

my $config_file= 'test.yml';

my $config_yaml = YAML::Tiny->new ( { font => $data } );

$config_yaml->write($config_file);

my $new_config = YAML::Tiny->read($config_file) ;
YYY $new_config;

devel$ cat test.yml
---
font:
  -family: 'Courier 10 Pitch'
  -overstrike: 0
  -size: -13
  -slant: roman
  -underline: 0
  -weight: normal

devel$ perl test-yaml.pl
YAML::Tiny found illegal characters in plain scalar: 'family: 'Courier 10 Pitch'' at test-yaml.pl line 21.
  • Loading branch information
Dominique Dumont committed Jul 5, 2017
1 parent 0492d64 commit 03a0111
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/YAML/Tiny.pm
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ sub _load_hash {
$hash->{$key} = undef;
return 1;
}
if ( $lines->[0] =~ /^(\s*)-/ ) {
if ( $lines->[0] =~ /^(\s*)-(?:\s|$)/ ) {
$hash->{$key} = [];
$self->_load_array(
$hash->{$key}, [ @$indent, length($1) ], $lines
Expand Down
6 changes: 6 additions & 0 deletions t/10_read.t
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ my %passes = (
],
utf8 => 'author',
},
'hash with key' => {
file => 'font-data.yml',
perl => [
{ font => { -family => 'Courier 10 Pitch', -overstrike => 0 } }
]
}
);

for my $key ( sort keys %passes ) {
Expand Down
4 changes: 4 additions & 0 deletions t/data/font-data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
font:
-family: 'Courier 10 Pitch'
-overstrike: 0

0 comments on commit 03a0111

Please sign in to comment.