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 => ['+', $include] );
# we need the original YAML::PP object for getting the current filename
# and for loading another file
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 => ['+', $include] );
# /path/to/include/yaml/1/file1.yaml
# ---
# a: b
my $yaml = <<'EOM';
- included: !include file1.yaml
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:

    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.

2022-11-12 perl v5.36.0