Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix error with hash keys beginning with '-' #51

Closed
wants to merge 2 commits into from

Conversation

dod38fr
Copy link

@dod38fr dod38fr commented Jul 5, 2017

Hello

YAML::Tiny fails to parse a hash where the key begins with '-' (e.g. '-foo'). YAML::Tiny fails with:

YAML::Tiny found illegal characters in plain scalar: 'family: 'Courier 10 Pitch'' at test-yaml.pl line 21.

This PR contains a fix to this problem and a new test case for this style of hash keys.

Here's the test program that triggers this error:

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

YYY (YAML::Tiny->read('test.yml') );

The yaml file:

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

The result of the script with current version of YAML::Tiny:

$ perl test-yaml.pl
YAML::Tiny found illegal characters in plain scalar: 'family: 'Courier 10 Pitch'' at test-yaml.pl line 6.

And the result with the patch:

$ perl -Ilib test-yaml.pl
--- !!perl/array:YAML::Tiny
- font:
    -family: Courier 10 Pitch
    -overstrike: 0
    -size: -13
    -slant: roman
    -underline: 0
    -weight: normal
...
  at test-yaml.pl line 6

This patch also fixes RT #85045

All the best

@karenetheridge
Copy link
Member

Your example is a bit hard to follow because of the use of XXX, which also includes yaml encoding/decoding inside of it.

@karenetheridge
Copy link
Member

I suspect the problem here is not that keys starting with - cannot be used, but rather that they don't round-trip: parsing works fine when the key is denoted with quotes, but dumping the yaml does not add them:

perl -MYAML::Tiny=Load -MData::Dumper -wle'print Dumper(Load(qq{---\nfont:\n  "-family": 1\n}))'
$VAR1 = {
          'font' => {
                      '-family' => '1'
                    }
        };

perl -Ilib -MYAML::Tiny=Dump -wle'print Dump({font => { "-family" => 1 }})'
---
font:
  -family: 1

We'll need to look at what YAML::XS does to be sure.

@dod38fr
Copy link
Author

dod38fr commented Jul 7, 2017

According to YAML spec:

A block sequence is simply a series of nodes, each denoted by a leading “-” indicator. The “-” indicator must be separated from the node by white space. This allows “-” to be used as the first character in a plain scalar if followed by a non-space character (e.g. “-1”).

So I believe that the dump done by YAML::Tiny is fine: -family: 1 is a "block mapping" :

$ perl -MYAML=Load -MData::Dumper -wle 'print Dumper(Load(qq{---\nfont:\n  -family: 1\n}))'
$VAR1 = {
          'font' => {
                      '-family' => '1'
                    }
        };

"-family": 1 is also a "block mapping" with extra quotes:

$  perl -MYAML=Load -MData::Dumper -wle 'print Dumper(Load(qq{---\nfont:\n  "-family": 1\n}))'
$VAR1 = {
          'font' => {
                      '-family' => '1'
                    }
        };

whereas - family: 11 is "block mapping" in a "block sequence":

$ perl -MYAML=Load -MData::Dumper -wle 'print Dumper(Load(qq{---\nfont:\n  - family: 1\n}))'
$VAR1 = {
          'font' => [
                      {
                        'family' => '1'
                      }
                    ]
        };

The patch I propose is necessary for YAML::Tiny to behave like YAML:

$ perl -MYAML::Tiny=Load -MData::Dumper -wle 'print Dumper(Load(qq{---\nfont:\n  -family: 1\n}))'
YAML::Tiny found illegal characters in plain scalar: 'family: 1' at -e line 1.
$ perl -Ilib -MYAML::Tiny=Load -MData::Dumper -wle 'print Dumper(Load(qq{---\nfont:\n  -family: 1\n}))'
$VAR1 = {
          'font' => {
                    '-family' => '1'
                  }
        };

$ perl -MYAML=Load -MData::Dumper -wle 'print Dumper(Load(qq{---\nfont:\n  -family: 1\n}))'
$VAR1 = {
          'font' => {
                      '-family' => '1'
                    }
        };

All the best

Dominique Dumont and others added 2 commits October 1, 2021 17:17
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.
@karenetheridge karenetheridge deleted the branch Perl-Toolchain-Gang:devel December 15, 2024 21:22
@karenetheridge
Copy link
Member

Could you rebase this against master please? You should be able to reopen and adjust the merge base at the top of this page.

@dod38fr
Copy link
Author

dod38fr commented Dec 16, 2024

Hi
I've rebased my branch.

Unfortunately, I cannot re-open this PR.

Can you re-open it on your side ?

All the best

@karenetheridge
Copy link
Member

I cannot open it until/before the base branch has been changed (there is an option at the top of this PR page to do it) - it needs to be master, not devel.

@dod38fr
Copy link
Author

dod38fr commented Dec 17, 2024

I don't have the option to change the base branch after I click on "edit" button. I can only change the title of the PR.

I guess we're stuck.

Would you like me to open another PR with the right target branch ?

All the best

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants