1

I am using YAML::XS LoadFile method to load a YAML file in a perl script. Does this module support some kind of construct for including another YAML file? If no, is there another CPAN module that has this feature?

Worst case, I will need to define my own construct for this. Any pointers or tips?

shikhanshu
  • 1,466
  • 2
  • 16
  • 32
  • "_including another YAML file_" --- where? in what sense? Sure you can read it into the script, so get a Perl data structure for it, with which you can do all you want, For instance add it as needed to another Perl data structure obtained from the other YAML file. Also, YAML allows multiple documents in one file so you can just combine them that way. It's not clear what you want, and of course you haven't shown any code. Have you looked at [YAML](https://metacpan.org/pod/YAML) docs (and/or [YAML::Tiny](https://metacpan.org/pod/YAML::Tiny))? – zdim Jul 16 '20 at 21:54

2 Answers2

1

The short answer is that there's no official way in YAML to include a YAML file within another YAML file. See

How can I include a YAML file inside another?

Diab Jerius
  • 2,310
  • 13
  • 18
1

A late answer, but yes, you can do that in perl with YAML::PP::Include (disclaimer: I'm the author)

# /path/to/file.yaml
# ---
# included: !include include/file2.yaml
 
# /path/to/include/file2.yaml
# ---
# a: b
use YAML::PP::Schema::Include;
my $include = YAML::PP::Schema::Include->new;
 
my $yp = YAML::PP->new( schema => ['Core', $include] );
$include->yp($yp);
 
my ($data) = $yp->load_file("/path/to/file.yaml");

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

Also if you type in yaml include in metacpan you should get this module as a result: https://metacpan.org/search?size=20&q=yaml+include

Note that !include is not an official YAML tag, but any YAML library can choose to implement such a tag.

tinita
  • 3,987
  • 1
  • 21
  • 23