perlpunk / YAML-PP-p5

A YAML 1.2 processor in perl
https://metacpan.org/pod/YAML::PP
24 stars 8 forks source link

Question: is it possible to force all one-line string scalars to be single-quoted? #40

Open bpj opened 3 years ago

bpj commented 3 years ago

The title nearly says it all. The two additional points are that I would like also keys to be single-quoted and would prefer that keys/values containing only ASCII alphanumerics and SPACE would be exempt.

perlpunk commented 3 years ago

Sorry for the late answer, I have been extremely busy.

There are no options to customize quoting like that yet. What you can do is to preprocess the structure yourself and create objects that preserve quoting style:

use YAML::PP;
use YAML::PP::Common qw/ PRESERVE_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE /;        
my $yp = YAML::PP->new( preserve => PRESERVE_SCALAR_STYLE );
my $h = $yp->preserved_mapping({}); # We need this to be able to store objects as keys
my $scalar = $yp->preserved_scalar("my special string", style => YAML_SINGLE_QUOTED_SCALAR_STYLE );
$h->{ $scalar } = $scalar;
my $dump = $yp->dump_string($h);
say $dump;
__END__
---
'my special string': 'my special string'

Let me know if this works for you and if you find it useful or not.