GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
YAML::PP::Schema::Include(3) User Contributed Perl Documentation YAML::PP::Schema::Include(3)

YAML::PP::Schema::Include - Include YAML files

    # /path/to/file.yaml
    # ---
    # included: !include include/file2.yaml

    # /path/to/include/file2.yaml
    # ---
    # a: b

    my $include = YAML::PP::Schema::Include->new;

    my $yp = YAML::PP->new( schema => ['JSON', $include] );
    # we need the original YAML::PP object for getting the current filename
    # and for loading another file
    $include->yp($yp);

    my ($data) = $yp->load_file("/path/to/file.yaml");

    # The result will be:
    $data = {
        included => { a => 'b' }
    };

Allow absolute filenames and upwards '..':

    my $include = YAML::PP::Schema::Include->new(
        allow_absolute => 1, # default: 0
    );

Specify paths to search for includes:

    my @include_paths = ("/path/to/include/yaml/1", "/path/to/include/yaml/2");
    my $include = YAML::PP::Schema::Include->new(
        paths => \@include_paths,
    );
    my $yp = YAML::PP->new( schema => ['JSON', $include] );
    $include->yp($yp);

    # /path/to/include/yaml/1/file1.yaml
    # ---
    # a: b

    my $yaml = <<'EOM';
    - included: !include file1.yaml
    EOM
    my ($data) = $yp->load_string($yaml);

This plugin allows you to split a large YAML file into smaller ones. You can then include these files with the "!include" tag.

It will search for the specified filename relative to the currently processed filename.

You can also specify the paths where to search for files to include. It iterates through the paths and returns the first filename that exists.

By default, only relative paths are allowed. Any "../" in the path will be removed. You can change that behaviour by setting the option "allow_absolute" to true.

If the included file contains more than one document, only the first one will be included.

I will probably add a possibility to return all documents as an arrayref.

The included YAML file will be loaded by creating a new YAML::PP object with the schema from the existing object. This way you can recursively include files.

You can even reuse the same include via an alias:

    ---
    invoice:
        shipping address: &address !include address.yaml
        billing address: *address

Circular includes will be detected, and will be fatal.

It's possible to specify what to do with the included file:

    my $include = YAML::PP::Schema::Include->new(
        loader => sub {
            my ($yp, $filename);
            if ($filename =~ m/\.txt$/) {
                # open file and just return text
            }
            else {
                # default behaviour
                return $yp->load_file($filename);
            }
        },
    );

For example, RAML defines an "!include" tag which depends on the file content. If it contains a special RAML directive, it will be loaded as YAML, otherwise the content of the file will be included as a string.

So with this plugin you are able to read RAML specifications.

2021-12-25 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.