Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YAML::Dumper not quoting scalar string "-"

Tags:

ruby

yaml

perl

Problem: Getting Perl's YAML::Dumper to emit {key=>"-"} as key: "-" rather than key: -

I'm processing hashes from database rows, reading them with SOAP and turning them into YAML for consumption by the next stage.

The first stage is written in Perl, and generates the YAML using YAML::Dumper. Unfortunately, for keys that have a value of "-", the bare hyphen is emitted sans any quoting:

$ perl -e 'use YAML;use YAML::Dumper; my $ydumper=YAML::Dumper->new(); print $ydumper->dump({key1=>1,key2=>"-",key3=>3});'
--- 
key1: 1
key2: -
key3: 3

The next stage, which reads the above and performs further manipulations on it, is in Ruby. The raw dash causes Ruby's YAML parser to varf:

$ ruby -rubygems -ryaml -e 'YAML.load($stdin.read);'  # assume the above piped in
/usr/share/ruby/vendor_ruby/psych.rb:205:in `parse': (<unknown>): block sequence entries are not allowed in this context at line 3 column 7 (Psych::SyntaxError)
    from /usr/share/ruby/vendor_ruby/psych.rb:205:in `parse_stream'
    from /usr/share/ruby/vendor_ruby/psych.rb:153:in `parse'
    from /usr/share/ruby/vendor_ruby/psych.rb:129:in `load'
    from -e:1:in `<main>'

My question is simple: How can I instruct YAML::Dumper to emit the dash as a quoted string?

Thanks!

like image 877
RoUS Avatar asked Mar 19 '23 12:03

RoUS


1 Answers

I've created a pull request for YAML::Dumper on github, that hopefully gets accepted and fixes this problem.

like image 92
dwarring Avatar answered Mar 28 '23 06:03

dwarring