YAML::PP(3) User Contributed Perl Documentation YAML::PP(3)

YAML::PP - YAML 1.2 processor

WARNING: Most of the inner API is not stable yet.

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);
    # The loader offers JSON::PP::Boolean, boolean.pm or
    # perl 1/'' (currently default) for booleans
    my $ypp = YAML::PP->new(boolean => 'JSON::PP');
    my $ypp = YAML::PP->new(boolean => 'boolean');
    my $ypp = YAML::PP->new(boolean => 'perl');
    # 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

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>

    my $ypp = YAML::PP->new;
    # load booleans via boolean.pm
    my $ypp = YAML::PP->new( boolean => 'boolean' );
    # load booleans via JSON::PP::true/false
    my $ypp = YAML::PP->new( boolean => 'JSON::PP' );
    
    # 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 => 'JSON::PP',
        schema => ['Core'],
        cyclic_refs => 'fatal',
        indent => 4,
        header => 1,
        footer => 1,
        version_directive => 1,
    );

Options:

Values: "perl" (currently default), "JSON::PP", "boolean"

This option is for loading and dumping.

Note that when dumping, only the chosen boolean style will be recognized. So if you choose "JSON::PP", "boolean" objects will not be recognized as booleans and will be dumped as ordinary objects (if you enable the 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".

Default: 'allow' but will be switched to fatal in the future for safety!

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
    
Default: 1

Since version 0.026

This option is for loading.

NOTE: THIS OPTION WILL BE SET TO 0 IN THE NEXT RELEASE.

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
    
Default: 2

This option is for dumping.

Use that many spaces for indenting

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.

Default: 1

This option is for dumping.

Print document heaader "---"

Default: 0

This option is for dumping.

Print document footer "..."

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.

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.

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_ORDER PRESERVE_SCALAR_STYLE /;
    # 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 );
    # Combine, e.g. preserve order and scalar style
    my $yp = YAML::PP->new( preserve => PRESERVE_ORDER | PRESERVE_SCALAR_STYLE );
    

Do NOT rely on the internal implementation of it.

If you load the following input:

    ---
    z: 1
    a: 2
    ---
    - plain
    - 'single'
    - "double"
    - |
      literal
    ---
    block mapping:
      flow sequence: [a, b]
    flow mapping: {a: b}
    

with this code:

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

Then dumping it will return the same output. Only folded block scalars '>' cannot preserve the style yet.

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

Scalars will be returned as objects of an internal class ("YAML::PP::Preserve::Scalar") with overloading. If you assign to such a scalar, the object will be replaced by a simple scalar.

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

You can also pass 1 as a value. In this case all preserving options will be enabled, also if there are new options added in the future.

There are also methods to craete preserved nodes from scratch. See the "preserved_(scalar|mapping|sequence" "METHODS" below.

    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 read from a file, you should decode it, 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.

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

Strings will be loaded as unicode characters.

    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 will return Unicode characters.

So if you want to write that to a file (or pass to YAML::XS, for example), you typically encode it via "Encode::encode()".

    $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.

This will dump to a predefined writer. By default it will just use the YAML::PP::Writer and output a string.

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

Since version 0.024

Experimental. Please report bugs or let me know this is useful and works.

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.

    use YAML::PP::Common qw/ PRESERVE_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: |+
    ...

Since version 0.024

Experimental. Please report bugs or let me know this is useful and works.

With this you can define which nodes are dumped with the more compact flow style instead of block style.

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 /;
    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]

Returns or sets the loader object, by default YAML::PP::Loader

Returns or sets the dumper object, by default YAML::PP::Dumper

Returns or sets the schema object

Creates and returns the default schema

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.

Note that in scalar context, "Load" and "LoadFile" return the first document (like YAML::Syck), while YAML and YAML::XS return the last.

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

Works like "load_string".

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

Works like "load_file".

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

Works like "dump_string".

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

Works like "dump_file".

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

One of the three YAML 1.2 official schemas
One of the three YAML 1.2 official schemas.
One of the three YAML 1.2 official schemas. Default
Schema implementing the most common YAML 1.1 types
Serializing Perl objects and types
Serializing binary data
Deprecated. See option "preserve"
YAML 1.1 merge keys for mappings
Include other YAML files via "!include" tags

To make the parsing process faster, you can plugin the libyaml parser with YAML::PP::LibYAML.

The process of loading and dumping is split into the following steps:

    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.

The Lexer is reading the YAML stream into tokens. This makes it possible to generate syntax highlighted YAML output.

Note that the API to retrieve the tokens will change.

The Parser retrieves the tokens from the Lexer. The main YAML content is then parsed with the Grammar.
The Constructor creates a data structure from the Parser events.
The Loader combines the constructor and parser.
The Dumper will delegate to the Representer
The Representer will create Emitter events from the given data structure.
The Emitter creates a YAML stream.

Still TODO:

    ---
    [ a, b, c ]: value
    
    ---
    [ a, b, c: d ]
    # equals
    [ a, b, { c: d } ]
    
    ---
    key ends with two colons::: value
    
If you have valid YAML that's not parsed, or the other way round, please create an issue.
You will see line and column numbers in the error message. The column numbers might still be wrong in some cases.
The error messages need to be improved.
Currently loaded as single characters without validating

The Constructor now supports all three YAML 1.2 Schemas, Failsafe, JSON and Core. Additionally you can choose the schema for YAML 1.1 as "YAML1_1".

Too see what strings are resolved as booleans, numbers, null etc. look at <https://perlpunk.github.io/YAML-PP-p5/schema-examples.html>.

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:

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.

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.
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.
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:

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.

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

All the available parsers and loaders for Perl are behaving differently, and more important, aren't conforming to the spec. YAML::XS is doing pretty well, but "libyaml" only handles YAML 1.1 and diverges a bit from the spec. The pure perl loaders lack support for a number of features.

I was going over YAML.pm issues end of 2016, integrating old patches from rt.cpan.org and creating some pull requests myself. I realized that it would be difficult to patch YAML.pm to parse YAML 1.1 or even 1.2, and it would also break existing usages relying on the current behaviour.

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 I got the idea to write my own parser and started on New Year's Day 2017. Without the test suite and the editor I would have never started this.

I also started another YAML Test project 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 about 230 test cases and expected parsing events and more. There will be more tests coming. This test suite allows you to write parsers without turning the examples from the Specification into tests yourself. Also the examples aren't completely covering all cases - the test suite aims to do that.

The suite contains .tml files, and in a separate 'data' release you will find the content in separate files, if you can't or don't want to use TestML.

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

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.

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

I was curious to see how the different frameworks handle the test cases, so, using the test suite and the docker image, I wrote some code 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.io>

As of this writing, the test matrix only contains valid test cases. Invalid ones will be added.

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 answered countless questions about the YAML Specification.

<https://www.yaml.info>

The Perl Foundation <https://www.perlfoundation.org/> sponsored this project (and the YAML Test Suite) with a grant of 2500 USD in 2017-2018.

Copyright 2017-2020 by Tina Müller

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

2020-09-10 perl v5.34.0