YAML::PP(3)

๐Ÿฆ‹ YAML::PP(3) โ€” User Contributed Perl Documentation ๐Ÿฆ‹

okay so bestie, this is the manual page for YAML::PP โ€” the manual name is YAML::PP(3) โ€” and the whole vibe is: YAML 1.2 processor. Let's get into it ๐Ÿ’–

โœจ NAME โœจ

YAML::PP โ€” YAML 1.2 processor. periodt.

๐ŸŒˆ SYNOPSIS ๐ŸŒˆ

WARNING: Most of the inner API is not stable yet. bestie, do not build your whole personality on it.

Here are a few examples of the basic load and dump methods:

use YAML::PP;
my $ypp = YAML::PP->new;

my $yaml = <<'EOM';
--- # Document one is a mapping
name: Tina
age: 29
favourite language: Perl

--- # Document two is a sequence
- plain string
- 'in single quotes'
- "in double quotes we have escapes! like \t and \n"
- | # a literal block scalar
  line1
  line2
- > # a folded block scalar
  this is all one
  single line because the
  linebreaks will be folded
EOM

my @documents = $ypp->load_string($yaml);
my @documents = $ypp->load_file($filename);

my $yaml = $ypp->dump_string($data1, $data2);
$ypp->dump_file($filename, $data1, $data2);

# Enable perl data types and objects
my $ypp = YAML::PP->new(schema => [qw/ + Perl /]);
my $yaml = $yp->dump_string($data_with_perl_objects);

# Legacy interface
use YAML::PP qw/ Load Dump LoadFile DumpFile /;
my @documents = Load($yaml);
my @documents = LoadFile($filename);
my @documents = LoadFile($filehandle);
my $yaml = = Dump(@documents);
DumpFile($filename, @documents);
DumpFile($filenhandle @documents);

Some utility scripts, mostly useful for debugging:

# Load YAML into a data structure and dump with Data::Dumper
yamlpp-load < file.yaml

# Load and Dump
yamlpp-load-dump < file.yaml

# Print the events from the parser in yaml-test-suite format
yamlpp-events < file.yaml

# Parse and emit events directly without loading
yamlpp-parse-emit < file.yaml

# Create ANSI colored YAML. Can also be useful for invalid YAML, showing
# you the exact location of the error
yamlpp-highlight < file.yaml

๐ŸŒธ DESCRIPTION ๐ŸŒธ

YAML::PP is a modular YAML processor.

It aims to support "YAML 1.2" and "YAML 1.1". See https://yaml.org/. Some (rare) syntax elements are not yet supported and documented below.

YAML is a serialization language. The YAML input is called "YAML Stream". A stream consists of one or more "Documents", separated by a line with a document start marker ---. A document optionally ends with the document end marker ....

This allows one to process continuous streams additionally to a fixed input file or string.

The YAML::PP frontend will currently load all documents, and return only the first if called with scalar context.

The YAML backend is implemented in a modular way that allows one to add custom handling of YAML tags, perl objects and data types. The inner API is not yet stable. Suggestions welcome.

You can check out all current parse and load results from the yaml-test-suite here: https://perlpunk.github.io/YAML-PP-p5/test-suite.html

๐Ÿ‘‘ METHODS ๐Ÿ‘‘

๐Ÿงš new

my $ypp = YAML::PP->new;

# use YAML 1.2 Failsafe Schema
my $ypp = YAML::PP->new( schema => ['Failsafe'] );
# use YAML 1.2 JSON Schema
my $ypp = YAML::PP->new( schema => ['JSON'] );
# use YAML 1.2 Core Schema
my $ypp = YAML::PP->new( schema => ['Core'] );

# Die when detecting cyclic references
my $ypp = YAML::PP->new( cyclic_refs => 'fatal' );

my $ypp = YAML::PP->new(
    boolean => 'perl',
    schema => ['Core'],
    cyclic_refs => 'fatal',
    indent => 4,
    header => 1,
    footer => 0,
    version_directive => 0,
    require_footer => 0,
    max_depth => 512,
);

Options:

  • boolean โ€” Values: "perl" (currently default), "JSON::PP", "boolean", "perl_experimental".

This option is for loading and dumping.

In case of perl 5.36 and later, builtin booleans should work out of the box (since YAML::PP >= 0.38.0).

print YAML::PP->new->dump_string([ builtin::true, !1 ]);
# ---
# - true
# - false

For earlier perl versions, you can use "pseudo" booleans like documented in the following examples.

# load/dump booleans via boolean.pm
my $ypp = YAML::PP->new( boolean => 'boolean' );
# load/dump booleans via JSON::PP::true/false
my $ypp = YAML::PP->new( boolean => 'JSON::PP' );

You can also specify more than one class, comma separated. This is important for dumping.

boolean => 'JSON::PP,boolean'
Booleans will be loaded as JSON::PP::Booleans, but when dumping, also
'boolean' objects will be recognized

boolean => 'JSON::PP,*'
Booleans will be loaded as JSON::PP::Booleans, but when dumping, all
currently supported boolean classes will be recognized

boolean => '*'
Booleans will be loaded as perl booleans, but when dumping, all
currently supported boolean classes will be recognized

boolean => ''
Booleans will be loaded as perl booleans, but when dumping, nothing
will be recognized as booleans.
This option is for backwards compatibility for perl versions < 5.36,
if you rely on [!!1, !1] being dumped as [1, ''].

The option "perl_experimental" was introduced when experimental boolean support was added to perl 5.36. Since it will not be experimental anymore in perl 5.40 \o/ the option is deprecated and the same as "perl".

  • schema โ€” Default: "['Core']".

This option is for loading and dumping.

Array reference. Here you can define what schema to use. Supported standard Schemas are: "Failsafe", "JSON", "Core", "YAML1_1".

To get an overview how the different Schemas behave, see https://perlpunk.github.io/YAML-PP-p5/schemas.html

Additionally you can add further schemas, for example "Merge".

  • cyclic_refs โ€” Default: "fatal" (since 0.037).

Before the default was "allow", but this can lead to memory leaks when loading on untrusted data, so it was changed to "fatal" by default.

This option is for loading only.

Defines what to do when a cyclic reference is detected when loading.

# fatal  - die
# warn   - Just warn about them and replace with undef
# ignore - replace with undef
# allow  - Default
  • duplicate_keys โ€” Default: 0.

Since version 0.027.

This option is for loading.

The YAML Spec says duplicate mapping keys should be forbidden.

When set to true, duplicate keys in mappings are allowed (and will overwrite the previous key).

When set to false, duplicate keys will result in an error when loading.

This is especially useful when you have a longer mapping and don't see the duplicate key in your editor:

---
a: 1
b: 2
# .............
a: 23 # error
  • indent โ€” Default: 2.

This option is for dumping.

Use that many spaces for indenting.

  • width โ€” Since version 0.025. Default: 80.

This option is for dumping.

Maximum columns when dumping.

This is only respected when dumping flow collections right now.

in the future it will be used also for wrapping long strings.

  • header โ€” Default: 1.

This option is for dumping.

Print document header ---.

  • footer โ€” Default: 0.

Since version v0.39.

This option is for dumping.

Print document footer ....

  • require_footer โ€” Default: 0.

Will require a ... at the end of each document. This can be useful in a context where you want to make sure you received the complete content, for example over network.

# Good
---
a: 1
...
---
a: 2
...

# Bad
---
a: 1
---
a: 2
  • max_depth โ€” Since version v0.40. default: 512.

Loading deeply nested, but still small YAML documents, like

[[[[[[[[[[...]]]]]]]]]]

can be significantly slower than a "normal" document with the same size. By default the depth is limited to 512, which should be more than enough for 99% of the use cases.

  • yaml_version โ€” Since version 0.020.

This option is for loading and dumping.

Default: 1.2

Note that in this case, a directive %YAML 1.1 will basically be ignored and everything loaded with the "1.2 Core" Schema.

If you want to support both YAML 1.1 and 1.2, you have to specify that, and the schema ("Core" or "YAML1_1") will be chosen automatically.

my $yp = YAML::PP->new(
    yaml_version => ['1.2', '1.1'],
);

This is the same as

my $yp = YAML::PP->new(
    schema => ['+'],
    yaml_version => ['1.2', '1.1'],
);

because the "+" stands for the default schema per version.

When loading, and there is no %YAML directive, 1.2 will be considered as default, and the "Core" schema will be used.

If there is a "%YAML 1.1" directive, the "YAML1_1" schema will be used.

Of course, you can also make 1.1 the default:

my $yp = YAML::PP->new(
    yaml_version => ['1.1', '1.2'],
);

You can also specify 1.1 only:

my $yp = YAML::PP->new(
    yaml_version => ['1.1'],
);

In this case also documents with "%YAML 1.2" will be loaded with the "YAML1_1" schema.

  • version_directive โ€” Since version 0.020.

This option is for dumping.

Default: 0

Print Version Directive "%YAML 1.2" (or "%YAML 1.1") on top of each YAML document. It will use the first version specified in the "yaml_version" option.

  • preserve โ€” Since version 0.021. Default: false.

This option is for loading and dumping.

Preserving scalar styles is still experimental.

use YAML::PP::Common qw/ :PRESERVE /;

# Preserve the order of hash keys
my $yp = YAML::PP->new( preserve => PRESERVE_ORDER );

# Preserve the quoting style of scalars
my $yp = YAML::PP->new( preserve => PRESERVE_SCALAR_STYLE );

# Preserve block/flow style (since 0.024)
my $yp = YAML::PP->new( preserve => PRESERVE_FLOW_STYLE );

# Preserve alias names (since 0.027)
my $yp = YAML::PP->new( preserve => PRESERVE_ALIAS );

So like, you can totally mix and match them too:

# Combine, e.g. preserve order and scalar style
my $yp = YAML::PP->new( preserve => PRESERVE_ORDER | PRESERVE_SCALAR_STYLE );

But bestie, do NOT rely on the internal implementation of it. Like, that's the one thing. ๐Ÿ™ˆ

Okay so if you load this input:

---
z: 1
a: 2
---
- plain
- 'single'
- "double"
- |
  literal
- >
  folded
---
block mapping: &alias
  flow sequence: [a, b]
same mapping: *alias
flow mapping: {a: b}

with this code:

my $yp = YAML::PP->new(
    preserve => PRESERVE_ORDER | PRESERVE_SCALAR_STYLE
                | PRESERVE_FLOW_STYLE | PRESERVE_ALIAS
);
my ($hash, $styles, $flow) = $yp->load_file($file);
$yp->dump_file($hash, $styles, $flow);

then dumping it will give you back the exact same output. Round trip, no cap. โœจ

๐Ÿ‘‘ Heads up though: YAML lets you define anchors more than once. YAML::PP cannot preserve that right now. Example:
---
- &seq [a]
- *seq
- &seq [b]
- *seq

Because the data could get shuffled before it's dumped again, the anchor definition could break. In that case, repeated anchor names get discarded when loading and dumped with numeric anchors like usual. Not the vibe, but it is what it is.

Implementation (iykyk):

When loading, hashes will be tied to an internal class (YAML::PP::Preserve::Hash) that keeps the key order.

Scalars get returned as objects of an internal class (YAML::PP::Preserve::Scalar) with overloading. If you assign to such a scalar, the object gets replaced by a plain old scalar. ๐Ÿ’€

# assignment, style gets lost
$styles->[1] .= ' append';

You can also pass 1 as a value, and in that case all the preserving options get enabled โ€” including any new options added in the future. Future-proof queen behavior. ๐Ÿ’…

There are also methods to create preserved nodes from scratch, so go check preserved_(scalar|mapping|sequence) in the "METHODS" below. ๐Ÿซถ


๐Ÿฆ‹ load_string

my $doc = $ypp->load_string("foo: bar");
my @docs = $ypp->load_string("foo: bar\n---\n- a");

Input should be Unicode characters. So if you're reading from a file, decode it first โ€” for example with Encode::decode().

โญ Note that in scalar context, load_string and load_file return the first document (like YAML::Syck), while YAML and YAML::XS return the last. Different kings, different rules. ๐Ÿ‘‘

๐ŸŒธ load_file

my $doc = $ypp->load_file("file.yaml");
my @docs = $ypp->load_file("file.yaml");

Strings will be loaded as unicode characters. Cute and correct.

๐ŸŽ€ dump_string

my $yaml = $ypp->dump_string($doc);
my $yaml = $ypp->dump_string($doc1, $doc2);
my $yaml = $ypp->dump_string(@docs);

Input strings should be Unicode characters. Output comes back as Unicode characters.

So if you wanna write that to a file (or pass it to YAML::XS, for example), you typically encode it via Encode::encode(). Encode on the way out, decode on the way in โ€” the circle of life. ๐Ÿฆ‹

๐Ÿ’œ dump_file

$ypp->dump_file("file.yaml", $doc);
$ypp->dump_file("file.yaml", $doc1, $doc2);
$ypp->dump_file("file.yaml", @docs);

Input data should be Unicode characters.

โœจ dump

This one dumps to a predefined writer. By default it just uses YAML::PP::Writer and outputs a string.

my $writer = MyWriter->new(\my $output);
my $yp = YAML::PP->new(
    writer => $writer,
);
$yp->dump($data);

๐Ÿ“ preserved_scalar

Since version 0.024.

Experimental. Please report bugs or let me know this is useful and works. ๐Ÿ™

With this you can define a certain scalar style when dumping data. Figuring out the best style is a hard task and practically impossible to get it right for all cases. It's also a matter of taste. (And we respect all tastes here. ๐Ÿ’…)

use YAML::PP::Common qw/ PRESERVE_SCALAR_STYLE YAML_LITERAL_SCALAR_STYLE /;
my $yp = YAML::PP->new(
    preserve => PRESERVE_SCALAR_STYLE,
);
# a single linebreak would normally be dumped with double quotes: "\n"
my $scalar = $yp->preserved_scalar("\n", style => YAML_LITERAL_SCALAR_STYLE );

my $data = { literal => $scalar };
my $dump = $yp->dump_string($data);
# output
---
literal: |+

...

๐ŸŒˆ preserved_mapping, preserved_sequence

Since version 0.024.

Experimental. Please report bugs or let me know this is useful and works. ๐Ÿ™

With this you can define which nodes get dumped with the more compact flow style instead of block style. Compact = slay.

If you add PRESERVE_ORDER to the preserve option, it will also keep the order of the keys in a hash. ๐Ÿ”ฅ

use YAML::PP::Common qw/
    PRESERVE_ORDER PRESERVE_FLOW_STYLE
    YAML_FLOW_MAPPING_STYLE YAML_FLOW_SEQUENCE_STYLE
/;
my $yp = YAML::PP->new(
    preserve => PRESERVE_FLOW_STYLE | PRESERVE_ORDER
);

my $hash = $yp->preserved_mapping({}, style => YAML_FLOW_MAPPING_STYLE);
# Add values after initialization to preserve order
%$hash = (z => 1, a => 2, y => 3, b => 4);

my $array = $yp->preserved_sequence([23, 24], style => YAML_FLOW_SEQUENCE_STYLE);

my $data = $yp->preserved_mapping({});
%$data = ( map => $hash, seq => $array );

my $dump = $yp->dump_string($data);
# output
---
map: {z: 1, a: 2, y: 3, b: 4}
seq: [23, 24]

๐Ÿงš loader

Returns or sets the loader object โ€” by default YAML::PP::Loader.

โญ dumper

Returns or sets the dumper object โ€” by default YAML::PP::Dumper.

๐ŸŽ€ schema

Returns or sets the schema object.

๐Ÿ’– default_schema

Creates and returns the default schema.


๐ŸŒŸ FUNCTIONS ๐ŸŒŸ

The functions Load, LoadFile, Dump and DumpFile are provided as a drop-in replacement for other existing YAML processors. No function is exported by default, so you gotta ask nicely with qw/ ... /. ๐Ÿ’…

โญ And again, bestie: in scalar context, Load and LoadFile return the first document (like YAML::Syck), while YAML and YAML::XS return the last.

๐Ÿฆ‹ Load

use YAML::PP qw/ Load /;
my $doc = Load($yaml);
my @docs = Load($yaml);

Works like load_string.

๐ŸŒธ LoadFile

use YAML::PP qw/ LoadFile /;
my $doc = LoadFile($file);
my @docs = LoadFile($file);
my @docs = LoadFile($filehandle);

Works like load_file. Yes, it takes filehandles too, we love a flexible icon. โœจ

๐Ÿ’œ Dump

use YAML::PP qw/ Dump /;
my $yaml = Dump($doc);
my $yaml = Dump(@docs);

Works like dump_string.

๐Ÿ“ DumpFile

use YAML::PP qw/ DumpFile /;
DumpFile($file, $doc);
DumpFile($file, @docs);
DumpFile($filehandle, @docs);

Works like dump_file.


๐Ÿงš PLUGINS ๐Ÿงš

You can alter the behaviour of YAML::PP by using the following schema classes:

  • YAML::PP::Schema::Failsafe โ€” one of the three YAML 1.2 official schemas.
  • YAML::PP::Schema::JSON โ€” one of the three YAML 1.2 official schemas.
  • YAML::PP::Schema::Core โ€” one of the three YAML 1.2 official schemas. This is the default, the main character. ๐Ÿ‘‘
  • YAML::PP::Schema::YAML1_1 โ€” schema implementing the most common YAML 1.1 types.
  • YAML::PP::Schema::Perl โ€” serializing Perl objects and types.
  • YAML::PP::Schema::Binary โ€” serializing binary data.
  • YAML::PP::Schema::Merge โ€” YAML 1.1 merge keys for mappings.
  • YAML::PP::Schema::Catchall โ€” Experimental. It was accidentally added in 0.38.1, and unknown tags forbidden. This was reverted in 0.39.0.

By default they will result in an error. ๐Ÿ˜ญ

  • YAML::PP::Schema::Include โ€” include other YAML files via !include tags.
  • YAML::PP::Schema::Tie::IxHash โ€” Deprecated. See option preserve. It's giving legacy. Use the new thing bestie.

And to make the parsing process faster, you can plugin the libyaml parser with YAML::PP::LibYAML. ๐Ÿš€ Speeeeed.

๐Ÿ› ๏ธ IMPLEMENTATION ๐Ÿ› ๏ธ

okay so here's the whole pipeline, bestie โ€” loading and dumping are each split into steps and it's honestly kind of iconic:

Load:

YAML Stream        Tokens        Event List        Data Structure
          --------->    --------->        --------->
            lex           parse           construct

Dump:

Data Structure       Event List        YAML Stream
          --------->        --------->
          represent           emit

You can dump basic perl types like hashes, arrays, scalars (strings, numbers). For dumping blessed objects and things like coderefs have a look at YAML::PP::Perl/YAML::PP::Schema::Perl ๐Ÿ’–

  • YAML::PP::Lexer โ€” the Lexer reads the YAML stream into tokens, which is what makes it possible to generate syntax highlighted YAML output. Heads up though: the API to retrieve the tokens will change ๐Ÿ‘€
  • YAML::PP::Parser โ€” the Parser grabs the tokens from the Lexer, and then the main YAML content gets parsed with the Grammar.
  • YAML::PP::Grammar โ€” it's here, it's part of the squad ๐ŸŽ€
  • YAML::PP::Constructor โ€” the Constructor builds a data structure out of the Parser events.
  • YAML::PP::Loader โ€” the Loader is literally just the constructor + parser combo. teamwork!
  • YAML::PP::Dumper โ€” the Dumper delegates to the Representer.
  • YAML::PP::Representer โ€” the Representer creates Emitter events from the data structure you hand it.
  • YAML::PP::Emitter โ€” the Emitter creates a YAML stream. final boss โœจ

๐Ÿฆ‹ YAML::PP::Parser ๐Ÿฆ‹

Still TODO:

  • Implicit collection keys โ€” yeah this one's still pending ๐Ÿ˜ญ
---
[ a, b, c ]: value
  • Implicit mapping in flow style sequences โ€” this one's supported since 0.029 (except some not relevant cases, iykyk):
---
[ a, b, c: d ]
# equals
[ a, b, { c: d } ]
  • Plain mapping keys ending with colons โ€” this was implemented in 0.037 ๐ŸŽ‰
---
key ends with two colons::: value

โœจ Supported Characters โœจ

If you have valid YAML that's not parsed, or the other way round, please create an issue ๐Ÿซถ

๐Ÿ“ Line and Column Numbers ๐Ÿ“

You will see line and column numbers in the error message. The column numbers might still be wrong in some cases โ€” no cap, we're working on it ๐Ÿ’…

๐Ÿ˜ญ Error Messages ๐Ÿ˜ญ

The error messages need to be improved. fr fr.

๐ŸŒธ Unicode Surrogate Pairs ๐ŸŒธ

Currently loaded as single characters without validating.

โญ Possibly more โญ

...yeah, possibly more. we stay humble.


๐ŸŽ€ YAML::PP::Constructor ๐ŸŽ€

The Constructor now supports all three YAML 1.2 Schemas โ€” Failsafe, JSON and Core. Plus you can choose the schema for YAML 1.1 as YAML1_1 ๐Ÿ’œ

To see what strings are resolved as booleans, numbers, null etc. look at https://perlpunk.github.io/YAML-PP-p5/schema-examples.html โ€” it's giving cheat sheet, obsessed.

You can choose the Schema like this:

my $ypp = YAML::PP->new(schema => ['JSON']); # default is 'Core'

The Tags !!seq and !!map are still ignored for now ๐Ÿ‘‘

It supports:

๐Ÿ”— Handling of Anchors/Aliases ๐Ÿ”—

Like in modules like YAML, the Constructor will use references for mappings and sequences โ€” but obviously not for scalars.

YAML::XS uses real aliases, which allows also aliasing scalars. I might add an option for that since aliasing is now available in pure perl ๐Ÿ‘€

โœ… Boolean Handling โœ…

You can choose between 'perl' (1/'', currently default), 'JSON::PP' and boolean.pm for handling boolean types. That allows you to dump the data structure with one of the JSON modules without losing information about booleans. slay.

๐Ÿ”ข Numbers ๐Ÿ”ข

Numbers are created as real numbers instead of strings, so that they are dumped correctly by modules like JSON::PP or JSON::XS, for example.

๐Ÿงฉ Complex Keys ๐Ÿงฉ

Mapping Keys in YAML can be more than just scalars. Of course, you can't load that into a native perl structure. The Constructor will stringify those keys with Data::Dumper instead of just returning something like HASH(0x55dc1b5d0178) ๐Ÿ™ˆ

Example:

use YAML::PP;
use JSON::PP;
my $ypp = YAML::PP->new;
my $coder = JSON::PP->new->ascii->pretty->allow_nonref->canonical;
my $yaml = <<'EOM';
complex:
    ?
        ?
            a: 1
            c: 2
        : 23
    : 42
EOM
my $data = $yppl->load_string($yaml);
say $coder->encode($data);
__END__
{
   "complex" : {
      "{'{a => 1,c => 2}' => 23}" : 42
   }
}

๐Ÿšง TODO ๐Ÿšง

Parse Tree โ€” I would like to generate a complete parse tree, that allows you to manipulate the data structure and also dump it, including all whitespaces and comments. The spec says that this is throwaway content, but I read that many people wish to be able to keep the comments. bestie, the dream ๐Ÿ˜ญโœจ


๐Ÿ’… YAML::PP::Dumper, YAML::PP::Emitter ๐Ÿ’…

The Dumper should be able to dump strings correctly, adding quotes whenever a plain scalar would look like a special string, like "true", or when it contains or starts with characters that are not allowed.

Most strings will be dumped as plain scalars without quotes. If they contain special characters or have a special meaning, they will be dumped with single quotes. If they contain control characters, including "\n", they will be dumped with double quotes.

It will recognize JSON::PP::Boolean and boolean.pm objects and dump them correctly ๐Ÿ”ฅ

Numbers which also have a "PV" flag will be recognized as numbers and not as strings:

my $int = 23;
say "int: $int"; # $int will now also have a PV flag

That means that if you accidentally use a string in numeric context, it will also be recognized as a number:

my $string = "23";
my $something = $string + 0;
print $yp->dump_string($string);
# will be emitted as an integer without quotes!

The layout is like libyaml output:

key:
- a
- b
- c
---
- key1: 1
  key2: 2
  key3: 3
---
- - a1
  - a2
- - b1
  - b2

๐Ÿ’ฌ FAQ - Frequently Asked Questions ๐Ÿ’ฌ

Are << merge keys supported?

Yes, this can be enabled optionally, see YAML::PP::Schema::Merge ๐Ÿ’–

Is there a linter / formatter for YAML

There is the widely used yamllint https://yamllint.readthedocs.io/, based on python's PyYAML. It is very configurable and will report errors or warnings. It cannot format.

Now there is also YAML::Tidy, which will format the given file according to your configuration. So far only a few configuration options exist, but they can already be quite helpful ๐ŸŽ€


๐Ÿค” Which YAML module should I use? ๐Ÿค”

There are many YAML modules on CPAN. For historical reasons some of them aren't handling YAML correctly.

Most of them are not compatible with the YAML spec and with each other, meaning they can interpret the same YAML differently.

The behaviours we are discussing here can be divided into parsing issues (syntax) and loading/constructing issues (for example type resolving which decides what is a number, boolean or null).

See also https://matrix.yaml.info/ (parsing) and https://perlpunk.github.io/YAML-PP-p5/schema-examples.html (loading). ๐Ÿ‘‘

YAML.pm

It was written even before the YAML 1.0 spec was finished and by that enabled perl users to process YAML very early. It might work for you if you have simple data, but it's missing quite some features and can also produce YAML that doesn't roundtrip. Nowadays it might be a good idea to switch. ๐Ÿ‘‹

YAML::XS

A libyaml binding that is robust and widely used. However, there are two things to consider.

  1. (syntax) libyaml diverged from the spec in several aspects. They are rare though.
  2. The type resolving does not adhere to YAML 1.1 or YAML 1.2, meaning it is incompatible with other YAML libraries in perl or other languages.

YAML::Tiny

It implements both a tiny subset of YAML, but also a superset. Meaning it will happily accept some YAML documents that are not officially valid. Type resolving is also not implemented according to the spec. ๐Ÿ˜ฌ

YAML::Syck

A binding to libsyck. It is even less compatible to YAML than libyaml. Also type resolving is not implemented according to the spec.

YAML::PP

Regarding YAML syntax, it is the second most YAML 1.2 compatible perl module. The cases it cannot (yet) parse are not relevant in perl programming, e.g. hash keys that are not strings. Regarding type resolving, it is compatible with the YAML 1.2 Core schema, so it should be possible to exchange data as YAML with other libraries in other languages. One downside is that it is the slowest perl YAML module ๐Ÿ’€ (but she's thorough, okay?)

YAML::Parser

This is a parser generated by the YAML grammar, and it's passing all official tests. A YAML::PP::Ref frontend exists that you can use just like YAML::PP. It is quite slow (although it might be ok for small files depending on the use case). The error messages it creates on invalid YAML are not helpful currently ๐Ÿ˜ญ

YAML::PP::LibYAML

This combines the YAML::LibYAML::API binding for parsing with the YAML::PP frontend for loading and type resolving. It is faster than YAML::PP but slower than YAML::XS. The divergence from the YAML spec regarding syntax is usually not a problem, and at the same time you have the advantage of being compatible to the YAML 1.2 Core Schema. icon behavior honestly ๐ŸŒˆ

๐Ÿ’ญ WHY

Why would someone even start writing a whole new YAML module? Okay bestie, gather 'round, because this is a story.

So here's the tea: literally all the available parsers and loaders for Perl are out here behaving differently from each other, and โ€” more importantly โ€” they just aren't conforming to the spec. Not the vibe. YAML::XS is doing pretty well, ngl, but "libyaml" only handles YAML 1.1 and diverges a bit from the spec. And the pure perl loaders? They lack support for a number of features. ๐Ÿ’€

So picture this: end of 2016, our author was going over YAML.pm issues, integrating old patches from rt.cpan.org and creating some pull requests personally. And it hit them: it would be difficult to patch YAML.pm to parse YAML 1.1 or even 1.2 โ€” and doing so would also break existing usages relying on the current behaviour. Big yikes. So patching was lowkey not it.

Then in 2016 Ingy dรถt Net initiated two really cool projects:

  • "YAML TEST SUITE"
  • "YAML EDITOR"

These projects are a big help for any developer. So the idea to write a parser was born, and work started on New Year's Day 2017. And fr fr โ€” without the test suite and the editor, this would have never been started. ๐ŸŽ€

There's also another YAML Test project that was started, which allows one to get a quick overview of which frameworks support which YAML features:

  • "YAML TEST MATRIX"

๐Ÿงช YAML TEST SUITE

https://github.com/yaml/yaml-test-suite

It contains almost 400 test cases and expected parsing events and more. And there will be more tests coming. The way this test suite allows you to write parsers without turning the examples from the Specification into tests yourself? Iconic. Because also the examples aren't completely covering all cases โ€” the test suite aims to do that.

Thanks also to Felix Krause, who is writing a YAML parser in Nim. He turned all the spec examples into test cases. King behaviour. ๐Ÿ‘‘

๐ŸŽฎ YAML EDITOR

This is a tool to play around with several YAML parsers and loaders in vim.

https://github.com/yaml/yaml-editor

The project contains the code to build the frameworks (16 as of this writing) and put it into one big Docker image.

It also contains the yaml-editor itself, which will start a vim in the docker container. It uses a lot of funky vimscript that makes playing with it easy and useful. You can choose which frameworks you want to test and see the output in a grid of vim windows. Literally so satisfying. โœจ

Especially when writing a parser, it is extremely helpful to have all the test cases and be able to play around with your own examples to see how they are handled.

๐Ÿงฉ YAML TEST MATRIX

The author was curious to see how the different frameworks handle the test cases, so โ€” using the test suite and the docker image โ€” some code was written that runs the tests, manipulates the output to compare it with the expected output, and created a matrix view.

https://github.com/perlpunk/yaml-test-matrix

You can find the latest build at https://matrix.yaml.info


๐Ÿซถ CONTRIBUTORS

  • Ingy dรถt Net โ€” Ingy is one of the creators of YAML. In 2016 he started the YAML Test Suite and the YAML Editor. He also made useful suggestions on the class hierarchy of YAML::PP.
  • Felix "flyx" Krause โ€” Felix answered countless questions about the YAML Specification. Absolute legend for that, honestly. ๐Ÿ™

๐Ÿ‘€ SEE ALSO


๐Ÿ’ธ SPONSORS

The Perl Foundation https://www.perlfoundation.org/ sponsored this project (and the YAML Test Suite) with a grant of 2500 USD in 2017-2018. We stan. ๐Ÿ’–


Copyright 2017-2022 by Tina Mรผller

This library is free software and may be distributed under the same terms as perl itself.


And that's the whole page, bestie! ๐Ÿฆ‹ This one's from perl v5.42.0, dated 2026-04-24, and it's YAML::PP(3) โ€” go forth and parse responsibly. ๐Ÿ’…๐ŸŒธ
๐Ÿ“ฆ where this page comes from