1YAML::PP::Schema::IncluUdsee(r3)Contributed Perl DocumenYtAaMtLi:o:nPP::Schema::Include(3)
2
3
4
6 YAML::PP::Schema::Include - Include YAML files
7
9 # /path/to/file.yaml
10 # ---
11 # included: !include include/file2.yaml
12
13 # /path/to/include/file2.yaml
14 # ---
15 # a: b
16
17 my $include = YAML::PP::Schema::Include->new;
18
19 my $yp = YAML::PP->new( schema => ['JSON', $include] );
20 # we need the original YAML::PP object for getting the current filename
21 # and for loading another file
22 $include->yp($yp);
23
24 my ($data) = $yp->load_file("/path/to/file.yaml");
25
26 # The result will be:
27 $data = {
28 included => { a => 'b' }
29 };
30
31 Allow absolute filenames and upwards '..':
32
33 my $include = YAML::PP::Schema::Include->new(
34 allow_absolute => 1, # default: 0
35 );
36
37 Specify paths to search for includes:
38
39 my @include_paths = ("/path/to/include/yaml/1", "/path/to/include/yaml/2");
40 my $include = YAML::PP::Schema::Include->new(
41 paths => \@include_paths,
42 );
43 my $yp = YAML::PP->new( schema => ['JSON', $include] );
44 $include->yp($yp);
45
46 # /path/to/include/yaml/1/file1.yaml
47 # ---
48 # a: b
49
50 my $yaml = <<'EOM';
51 - included: !include file1.yaml
52 EOM
53 my ($data) = $yp->load_string($yaml);
54
56 This plugin allows you to split a large YAML file into smaller ones.
57 You can then include these files with the "!include" tag.
58
59 It will search for the specified filename relative to the currently
60 processed filename.
61
62 You can also specify the paths where to search for files to include. It
63 iterates through the paths and returns the first filename that exists.
64
65 By default, only relative paths are allowed. Any "../" in the path will
66 be removed. You can change that behaviour by setting the option
67 "allow_absolute" to true.
68
69 If the included file contains more than one document, only the first
70 one will be included.
71
72 I will probably add a possibility to return all documents as an
73 arrayref.
74
75 The included YAML file will be loaded by creating a new YAML::PP object
76 with the schema from the existing object. This way you can recursively
77 include files.
78
79 You can even reuse the same include via an alias:
80
81 ---
82 invoice:
83 shipping address: &address !include address.yaml
84 billing address: *address
85
86 Circular includes will be detected, and will be fatal.
87
88 It's possible to specify what to do with the included file:
89
90 my $include = YAML::PP::Schema::Include->new(
91 loader => sub {
92 my ($yp, $filename);
93 if ($filename =~ m/\.txt$/) {
94 # open file and just return text
95 }
96 else {
97 # default behaviour
98 return $yp->load_file($filename);
99 }
100 },
101 );
102
103 For example, RAML defines an "!include" tag which depends on the file
104 content. If it contains a special RAML directive, it will be loaded as
105 YAML, otherwise the content of the file will be included as a string.
106
107 So with this plugin you are able to read RAML specifications.
108
109
110
111perl v5.30.1 2020-03-02 YAML::PP::Schema::Include(3)