| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# ABSTRACT: YAML 1.2 Processor |
|
2
|
42
|
|
|
42
|
|
2508501
|
use strict; |
|
|
42
|
|
|
|
|
75
|
|
|
|
42
|
|
|
|
|
1342
|
|
|
3
|
42
|
|
|
42
|
|
216
|
use warnings; |
|
|
42
|
|
|
|
|
68
|
|
|
|
42
|
|
|
|
|
3395
|
|
|
4
|
|
|
|
|
|
|
package YAML::PP; |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = 'v0.39.0'; # VERSION |
|
7
|
|
|
|
|
|
|
|
|
8
|
42
|
|
|
42
|
|
16498
|
use YAML::PP::Schema; |
|
|
42
|
|
|
|
|
90
|
|
|
|
42
|
|
|
|
|
1242
|
|
|
9
|
42
|
|
|
42
|
|
16166
|
use YAML::PP::Schema::JSON; |
|
|
42
|
|
|
|
|
136
|
|
|
|
42
|
|
|
|
|
2135
|
|
|
10
|
42
|
|
|
42
|
|
13766
|
use YAML::PP::Loader; |
|
|
42
|
|
|
|
|
125
|
|
|
|
42
|
|
|
|
|
1291
|
|
|
11
|
42
|
|
|
42
|
|
15180
|
use YAML::PP::Dumper; |
|
|
42
|
|
|
|
|
133
|
|
|
|
42
|
|
|
|
|
1516
|
|
|
12
|
42
|
|
|
42
|
|
212
|
use Scalar::Util qw/ blessed /; |
|
|
42
|
|
|
|
|
60
|
|
|
|
42
|
|
|
|
|
1851
|
|
|
13
|
42
|
|
|
42
|
|
209
|
use Carp qw/ croak /; |
|
|
42
|
|
|
|
|
67
|
|
|
|
42
|
|
|
|
|
1514
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
42
|
|
|
42
|
|
209
|
use base 'Exporter'; |
|
|
42
|
|
|
|
|
57
|
|
|
|
42
|
|
|
|
|
47253
|
|
|
16
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ Load LoadFile Dump DumpFile /; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my %YAML_VERSIONS = ('1.1' => 1, '1.2' => 1); |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
|
22
|
755
|
|
|
755
|
1
|
5186985
|
my ($class, %args) = @_; |
|
23
|
|
|
|
|
|
|
|
|
24
|
755
|
|
|
|
|
1757
|
my $bool = delete $args{boolean}; |
|
25
|
755
|
100
|
|
|
|
2419
|
$bool = 'perl' unless defined $bool; |
|
26
|
755
|
|
100
|
|
|
2233
|
my $schemas = delete $args{schema} || ['+']; |
|
27
|
755
|
|
100
|
|
|
3035
|
my $cyclic_refs = delete $args{cyclic_refs} || 'fatal'; |
|
28
|
755
|
|
|
|
|
1277
|
my $indent = delete $args{indent}; |
|
29
|
755
|
|
|
|
|
1070
|
my $width = delete $args{width}; |
|
30
|
755
|
|
|
|
|
1036
|
my $writer = delete $args{writer}; |
|
31
|
755
|
|
|
|
|
1032
|
my $header = delete $args{header}; |
|
32
|
755
|
|
|
|
|
1130
|
my $footer = delete $args{footer}; |
|
33
|
755
|
|
|
|
|
938
|
my $require_footer = delete $args{require_footer}; |
|
34
|
755
|
|
|
|
|
982
|
my $duplicate_keys = delete $args{duplicate_keys}; |
|
35
|
755
|
|
|
|
|
2521
|
my $yaml_version = $class->_arg_yaml_version(delete $args{yaml_version}); |
|
36
|
755
|
|
|
|
|
1196
|
my $default_yaml_version = $yaml_version->[0]; |
|
37
|
755
|
|
|
|
|
974
|
my $version_directive = delete $args{version_directive}; |
|
38
|
755
|
|
|
|
|
928
|
my $preserve = delete $args{preserve}; |
|
39
|
755
|
|
|
|
|
976
|
my $parser = delete $args{parser}; |
|
40
|
|
|
|
|
|
|
my $emitter = delete $args{emitter} || { |
|
41
|
755
|
|
50
|
|
|
3734
|
indent => $indent, |
|
42
|
|
|
|
|
|
|
width => $width, |
|
43
|
|
|
|
|
|
|
writer => $writer, |
|
44
|
|
|
|
|
|
|
}; |
|
45
|
755
|
50
|
|
|
|
1569
|
if (keys %args) { |
|
46
|
0
|
|
|
|
|
0
|
die "Unexpected arguments: " . join ', ', sort keys %args; |
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
|
|
49
|
755
|
|
|
|
|
970
|
my %schemas; |
|
50
|
755
|
|
|
|
|
1275
|
for my $v (@$yaml_version) { |
|
51
|
757
|
|
|
|
|
892
|
my $schema; |
|
52
|
757
|
50
|
33
|
|
|
2100
|
if (blessed($schemas) and $schemas->isa('YAML::PP::Schema')) { |
|
53
|
0
|
|
|
|
|
0
|
$schema = $schemas; |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
else { |
|
56
|
757
|
|
|
|
|
3290
|
$schema = YAML::PP::Schema->new( |
|
57
|
|
|
|
|
|
|
boolean => $bool, |
|
58
|
|
|
|
|
|
|
yaml_version => $v, |
|
59
|
|
|
|
|
|
|
); |
|
60
|
757
|
|
|
|
|
2707
|
$schema->load_subschemas(@$schemas); |
|
61
|
|
|
|
|
|
|
} |
|
62
|
756
|
|
|
|
|
1862
|
$schemas{ $v } = $schema; |
|
63
|
|
|
|
|
|
|
} |
|
64
|
754
|
|
|
|
|
1172
|
my $default_schema = $schemas{ $default_yaml_version }; |
|
65
|
|
|
|
|
|
|
|
|
66
|
754
|
|
|
|
|
3900
|
my $loader = YAML::PP::Loader->new( |
|
67
|
|
|
|
|
|
|
schemas => \%schemas, |
|
68
|
|
|
|
|
|
|
cyclic_refs => $cyclic_refs, |
|
69
|
|
|
|
|
|
|
parser => $parser, |
|
70
|
|
|
|
|
|
|
default_yaml_version => $default_yaml_version, |
|
71
|
|
|
|
|
|
|
preserve => $preserve, |
|
72
|
|
|
|
|
|
|
duplicate_keys => $duplicate_keys, |
|
73
|
|
|
|
|
|
|
require_footer => $require_footer, |
|
74
|
|
|
|
|
|
|
); |
|
75
|
753
|
|
|
|
|
3566
|
my $dumper = YAML::PP::Dumper->new( |
|
76
|
|
|
|
|
|
|
schema => $default_schema, |
|
77
|
|
|
|
|
|
|
emitter => $emitter, |
|
78
|
|
|
|
|
|
|
header => $header, |
|
79
|
|
|
|
|
|
|
footer => $footer, |
|
80
|
|
|
|
|
|
|
version_directive => $version_directive, |
|
81
|
|
|
|
|
|
|
preserve => $preserve, |
|
82
|
|
|
|
|
|
|
); |
|
83
|
|
|
|
|
|
|
|
|
84
|
753
|
|
|
|
|
2399
|
my $self = bless { |
|
85
|
|
|
|
|
|
|
schema => \%schemas, |
|
86
|
|
|
|
|
|
|
loader => $loader, |
|
87
|
|
|
|
|
|
|
dumper => $dumper, |
|
88
|
|
|
|
|
|
|
}, $class; |
|
89
|
753
|
|
|
|
|
4161
|
return $self; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub clone { |
|
93
|
9
|
|
|
9
|
0
|
298
|
my ($self) = @_; |
|
94
|
9
|
|
|
|
|
78
|
my $clone = { |
|
95
|
|
|
|
|
|
|
schema => $self->schema, |
|
96
|
|
|
|
|
|
|
loader => $self->loader->clone, |
|
97
|
|
|
|
|
|
|
dumper => $self->dumper->clone, |
|
98
|
|
|
|
|
|
|
}; |
|
99
|
9
|
|
|
|
|
24
|
return bless $clone, ref $self; |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub _arg_yaml_version { |
|
103
|
755
|
|
|
755
|
|
1332
|
my ($class, $version) = @_; |
|
104
|
755
|
|
|
|
|
1727
|
my @versions = ('1.2'); |
|
105
|
755
|
100
|
|
|
|
1667
|
if (defined $version) { |
|
106
|
5
|
|
|
|
|
8
|
@versions = (); |
|
107
|
5
|
100
|
|
|
|
13
|
if (not ref $version) { |
|
108
|
1
|
|
|
|
|
2
|
$version = [$version]; |
|
109
|
|
|
|
|
|
|
} |
|
110
|
5
|
|
|
|
|
9
|
for my $v (@$version) { |
|
111
|
7
|
50
|
|
|
|
17
|
unless ($YAML_VERSIONS{ $v }) { |
|
112
|
0
|
|
|
|
|
0
|
croak "YAML Version '$v' not supported"; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
7
|
|
|
|
|
15
|
push @versions, $v; |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
} |
|
117
|
755
|
|
|
|
|
1554
|
return \@versions; |
|
118
|
|
|
|
|
|
|
} |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub loader { |
|
122
|
2207
|
50
|
|
2207
|
1
|
5649
|
if (@_ > 1) { |
|
123
|
0
|
|
|
|
|
0
|
$_[0]->{loader} = $_[1] |
|
124
|
|
|
|
|
|
|
} |
|
125
|
2207
|
|
|
|
|
7634
|
return $_[0]->{loader}; |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub dumper { |
|
129
|
1494
|
50
|
|
1494
|
1
|
3837
|
if (@_ > 1) { |
|
130
|
0
|
|
|
|
|
0
|
$_[0]->{dumper} = $_[1] |
|
131
|
|
|
|
|
|
|
} |
|
132
|
1494
|
|
|
|
|
5784
|
return $_[0]->{dumper}; |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub schema { |
|
136
|
16
|
50
|
|
16
|
1
|
75
|
if (@_ > 1) { $_[0]->{schema}->{'1.2'} = $_[1] } |
|
|
0
|
|
|
|
|
0
|
|
|
137
|
16
|
|
|
|
|
70
|
return $_[0]->{schema}->{'1.2'}; |
|
138
|
|
|
|
|
|
|
} |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub default_schema { |
|
141
|
3
|
|
|
3
|
1
|
14
|
my ($self, %args) = @_; |
|
142
|
|
|
|
|
|
|
my $schema = YAML::PP::Schema->new( |
|
143
|
|
|
|
|
|
|
boolean => $args{boolean}, |
|
144
|
3
|
|
|
|
|
26
|
); |
|
145
|
3
|
|
|
|
|
17
|
$schema->load_subschemas(qw/ Core /); |
|
146
|
3
|
|
|
|
|
15
|
return $schema; |
|
147
|
|
|
|
|
|
|
} |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub load_string { |
|
150
|
2176
|
|
|
2176
|
1
|
940594
|
my ($self, $yaml) = @_; |
|
151
|
2176
|
|
|
|
|
5528
|
return $self->loader->load_string($yaml); |
|
152
|
|
|
|
|
|
|
} |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub load_file { |
|
155
|
19
|
|
|
19
|
1
|
41
|
my ($self, $file) = @_; |
|
156
|
19
|
|
|
|
|
77
|
return $self->loader->load_file($file); |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub dump { |
|
160
|
1
|
|
|
1
|
1
|
6
|
my ($self, @data) = @_; |
|
161
|
1
|
|
|
|
|
3
|
return $self->dumper->dump(@data); |
|
162
|
|
|
|
|
|
|
} |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub dump_string { |
|
165
|
1478
|
|
|
1478
|
1
|
3239753
|
my ($self, @data) = @_; |
|
166
|
1478
|
|
|
|
|
3895
|
return $self->dumper->dump_string(@data); |
|
167
|
|
|
|
|
|
|
} |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub dump_file { |
|
170
|
6
|
|
|
6
|
1
|
16
|
my ($self, $file, @data) = @_; |
|
171
|
6
|
|
|
|
|
13
|
return $self->dumper->dump_file($file, @data); |
|
172
|
|
|
|
|
|
|
} |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# legagy interface |
|
175
|
|
|
|
|
|
|
sub Load { |
|
176
|
2
|
|
|
2
|
1
|
172223
|
my ($yaml) = @_; |
|
177
|
2
|
|
|
|
|
12
|
YAML::PP->new->load_string($yaml); |
|
178
|
|
|
|
|
|
|
} |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub LoadFile { |
|
181
|
3
|
|
|
3
|
1
|
1600
|
my ($file) = @_; |
|
182
|
3
|
|
|
|
|
10
|
YAML::PP->new->load_file($file); |
|
183
|
|
|
|
|
|
|
} |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub Dump { |
|
186
|
1
|
|
|
1
|
1
|
2103
|
my (@data) = @_; |
|
187
|
1
|
|
|
|
|
7
|
YAML::PP->new->dump_string(@data); |
|
188
|
|
|
|
|
|
|
} |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub DumpFile { |
|
191
|
4
|
|
|
4
|
1
|
921
|
my ($file, @data) = @_; |
|
192
|
4
|
|
|
|
|
22
|
YAML::PP->new->dump_file($file, @data); |
|
193
|
|
|
|
|
|
|
} |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub preserved_scalar { |
|
196
|
4
|
|
|
4
|
1
|
3712
|
my ($self, $value, %args) = @_; |
|
197
|
4
|
|
|
|
|
23
|
my $scalar = YAML::PP::Preserve::Scalar->new( |
|
198
|
|
|
|
|
|
|
value => $value, |
|
199
|
|
|
|
|
|
|
%args, |
|
200
|
|
|
|
|
|
|
); |
|
201
|
4
|
|
|
|
|
10
|
return $scalar; |
|
202
|
|
|
|
|
|
|
} |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub preserved_mapping { |
|
205
|
4
|
|
|
4
|
1
|
1609
|
my ($self, $hash, %args) = @_; |
|
206
|
4
|
|
|
|
|
8
|
my $data = {}; |
|
207
|
4
|
|
|
|
|
19
|
tie %$data, 'YAML::PP::Preserve::Hash'; |
|
208
|
4
|
|
|
|
|
17
|
%$data = %$hash; |
|
209
|
4
|
|
|
|
|
9
|
my $t = tied %$data; |
|
210
|
4
|
|
|
|
|
10
|
$t->{style} = $args{style}; |
|
211
|
4
|
|
|
|
|
8
|
$t->{alias} = $args{alias}; |
|
212
|
4
|
|
|
|
|
12
|
return $data; |
|
213
|
|
|
|
|
|
|
} |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
sub preserved_sequence { |
|
216
|
4
|
|
|
4
|
1
|
3743
|
my ($self, $array, %args) = @_; |
|
217
|
4
|
|
|
|
|
6
|
my $data = []; |
|
218
|
4
|
|
|
|
|
16
|
tie @$data, 'YAML::PP::Preserve::Array'; |
|
219
|
4
|
|
|
|
|
15
|
push @$data, @$array; |
|
220
|
4
|
|
|
|
|
7
|
my $t = tied @$data; |
|
221
|
4
|
|
|
|
|
6
|
$t->{style} = $args{style}; |
|
222
|
4
|
|
|
|
|
7
|
$t->{alias} = $args{alias}; |
|
223
|
4
|
|
|
|
|
11
|
return $data; |
|
224
|
|
|
|
|
|
|
} |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
package YAML::PP::Preserve::Hash; |
|
227
|
|
|
|
|
|
|
# experimental |
|
228
|
42
|
|
|
42
|
|
18409
|
use Tie::Hash; |
|
|
42
|
|
|
|
|
30102
|
|
|
|
42
|
|
|
|
|
1375
|
|
|
229
|
42
|
|
|
42
|
|
241
|
use base qw/ Tie::StdHash /; |
|
|
42
|
|
|
|
|
53
|
|
|
|
42
|
|
|
|
|
10903
|
|
|
230
|
42
|
|
|
42
|
|
281
|
use Scalar::Util qw/ reftype blessed /; |
|
|
42
|
|
|
|
|
59
|
|
|
|
42
|
|
|
|
|
21590
|
|
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
sub TIEHASH { |
|
233
|
34
|
|
|
34
|
|
70
|
my ($class, %args) = @_; |
|
234
|
34
|
|
|
|
|
154
|
my $self = bless { |
|
235
|
|
|
|
|
|
|
keys => [keys %args], |
|
236
|
|
|
|
|
|
|
data => { %args }, |
|
237
|
|
|
|
|
|
|
}, $class; |
|
238
|
|
|
|
|
|
|
} |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
sub STORE { |
|
241
|
127
|
|
|
127
|
|
980
|
my ($self, $key, $val) = @_; |
|
242
|
127
|
|
|
|
|
159
|
my $keys = $self->{keys}; |
|
243
|
127
|
100
|
|
|
|
180
|
unless (exists $self->{data}->{ $key }) { |
|
244
|
120
|
|
|
|
|
164
|
push @$keys, $key; |
|
245
|
|
|
|
|
|
|
} |
|
246
|
127
|
100
|
100
|
|
|
251
|
if (ref $val and not blessed($val)) { |
|
247
|
39
|
100
|
100
|
|
|
176
|
if (reftype($val) eq 'HASH' and not tied %$val) { |
|
|
|
100
|
100
|
|
|
|
|
|
248
|
1
|
|
|
|
|
2
|
tie %$val, 'YAML::PP::Preserve::Hash', %$val; |
|
249
|
|
|
|
|
|
|
} |
|
250
|
|
|
|
|
|
|
elsif (reftype($val) eq 'ARRAY' and not tied @$val) { |
|
251
|
2
|
|
|
|
|
5
|
tie @$val, 'YAML::PP::Preserve::Array', @$val; |
|
252
|
|
|
|
|
|
|
} |
|
253
|
|
|
|
|
|
|
} |
|
254
|
127
|
|
|
|
|
273
|
$self->{data}->{ $key } = $val; |
|
255
|
|
|
|
|
|
|
} |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
sub FIRSTKEY { |
|
258
|
91
|
|
|
91
|
|
3321
|
my ($self) = @_; |
|
259
|
91
|
|
|
|
|
266
|
return $self->{keys}->[0]; |
|
260
|
|
|
|
|
|
|
} |
|
261
|
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
sub NEXTKEY { |
|
263
|
320
|
|
|
320
|
|
1147
|
my ($self, $last) = @_; |
|
264
|
320
|
|
|
|
|
340
|
my $keys = $self->{keys}; |
|
265
|
320
|
|
|
|
|
431
|
for my $i (0 .. $#$keys) { |
|
266
|
1167
|
100
|
|
|
|
1401
|
if ("$keys->[ $i ]" eq "$last") { |
|
267
|
320
|
|
|
|
|
810
|
return $keys->[ $i + 1 ]; |
|
268
|
|
|
|
|
|
|
} |
|
269
|
|
|
|
|
|
|
} |
|
270
|
0
|
|
|
|
|
0
|
return; |
|
271
|
|
|
|
|
|
|
} |
|
272
|
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
sub FETCH { |
|
274
|
305
|
|
|
305
|
|
3009
|
my ($self, $key) = @_; |
|
275
|
305
|
|
|
|
|
582
|
my $val = $self->{data}->{ $key }; |
|
276
|
|
|
|
|
|
|
} |
|
277
|
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
sub DELETE { |
|
279
|
2
|
|
|
2
|
|
1805
|
my ($self, $key) = @_; |
|
280
|
2
|
|
|
|
|
4
|
@{ $self->{keys} } = grep { "$_" ne "$key" } @{ $self->{keys} }; |
|
|
2
|
|
|
|
|
17
|
|
|
|
12
|
|
|
|
|
26
|
|
|
|
2
|
|
|
|
|
9
|
|
|
281
|
2
|
|
|
|
|
14
|
delete $self->{data}->{ $key }; |
|
282
|
|
|
|
|
|
|
} |
|
283
|
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
sub EXISTS { |
|
285
|
34
|
|
|
34
|
|
794
|
my ($self, $key) = @_; |
|
286
|
34
|
|
|
|
|
64
|
return exists $self->{data}->{ $key }; |
|
287
|
|
|
|
|
|
|
} |
|
288
|
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
sub CLEAR { |
|
290
|
9
|
|
|
9
|
|
26
|
my ($self) = @_; |
|
291
|
9
|
|
|
|
|
18
|
$self->{keys} = []; |
|
292
|
9
|
|
|
|
|
43
|
$self->{data} = {}; |
|
293
|
|
|
|
|
|
|
} |
|
294
|
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
sub SCALAR { |
|
296
|
3
|
|
|
3
|
|
1562
|
my ($self) = @_; |
|
297
|
3
|
|
|
|
|
8
|
return scalar %{ $self->{data} }; |
|
|
3
|
|
|
|
|
13
|
|
|
298
|
|
|
|
|
|
|
} |
|
299
|
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
package YAML::PP::Preserve::Array; |
|
301
|
|
|
|
|
|
|
# experimental |
|
302
|
42
|
|
|
42
|
|
16000
|
use Tie::Array; |
|
|
42
|
|
|
|
|
39534
|
|
|
|
42
|
|
|
|
|
1215
|
|
|
303
|
42
|
|
|
42
|
|
231
|
use base qw/ Tie::StdArray /; |
|
|
42
|
|
|
|
|
60
|
|
|
|
42
|
|
|
|
|
10206
|
|
|
304
|
42
|
|
|
42
|
|
235
|
use Scalar::Util qw/ reftype blessed /; |
|
|
42
|
|
|
|
|
79
|
|
|
|
42
|
|
|
|
|
25632
|
|
|
305
|
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
sub TIEARRAY { |
|
307
|
16
|
|
|
16
|
|
30
|
my ($class, @items) = @_; |
|
308
|
16
|
|
|
|
|
43
|
my $self = bless { |
|
309
|
|
|
|
|
|
|
data => [@items], |
|
310
|
|
|
|
|
|
|
}, $class; |
|
311
|
16
|
|
|
|
|
34
|
return $self; |
|
312
|
|
|
|
|
|
|
} |
|
313
|
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
sub FETCH { |
|
315
|
99
|
|
|
99
|
|
169
|
my ($self, $i) = @_; |
|
316
|
99
|
|
|
|
|
210
|
return $self->{data}->[ $i ]; |
|
317
|
|
|
|
|
|
|
} |
|
318
|
|
|
|
|
|
|
sub FETCHSIZE { |
|
319
|
101
|
|
|
101
|
|
586
|
my ($self) = @_; |
|
320
|
101
|
|
|
|
|
111
|
return $#{ $self->{data} } + 1; |
|
|
101
|
|
|
|
|
292
|
|
|
321
|
|
|
|
|
|
|
} |
|
322
|
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
sub _preserve { |
|
324
|
42
|
|
|
42
|
|
58
|
my ($val) = @_; |
|
325
|
42
|
100
|
100
|
|
|
100
|
if (ref $val and not blessed($val)) { |
|
326
|
13
|
100
|
100
|
|
|
53
|
if (reftype($val) eq 'HASH' and not tied %$val) { |
|
|
|
100
|
100
|
|
|
|
|
|
327
|
5
|
|
|
|
|
9
|
tie %$val, 'YAML::PP::Preserve::Hash', %$val; |
|
328
|
|
|
|
|
|
|
} |
|
329
|
|
|
|
|
|
|
elsif (reftype($val) eq 'ARRAY' and not tied @$val) { |
|
330
|
1
|
|
|
|
|
3
|
tie @$val, 'YAML::PP::Preserve::Array', @$val; |
|
331
|
|
|
|
|
|
|
} |
|
332
|
|
|
|
|
|
|
} |
|
333
|
42
|
|
|
|
|
139
|
return $val; |
|
334
|
|
|
|
|
|
|
} |
|
335
|
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
sub STORE { |
|
337
|
6
|
|
|
6
|
|
769
|
my ($self, $i, $val) = @_; |
|
338
|
6
|
|
|
|
|
15
|
_preserve($val); |
|
339
|
6
|
|
|
|
|
15
|
$self->{data}->[ $i ] = $val; |
|
340
|
|
|
|
|
|
|
} |
|
341
|
|
|
|
|
|
|
sub PUSH { |
|
342
|
14
|
|
|
14
|
|
37
|
my ($self, @args) = @_; |
|
343
|
14
|
|
|
|
|
19
|
push @{ $self->{data} }, map { _preserve $_ } @args; |
|
|
14
|
|
|
|
|
37
|
|
|
|
31
|
|
|
|
|
50
|
|
|
344
|
|
|
|
|
|
|
} |
|
345
|
|
|
|
|
|
|
sub STORESIZE { |
|
346
|
1
|
|
|
1
|
|
10
|
my ($self, $i) = @_; |
|
347
|
1
|
|
|
|
|
3
|
$#{ $self->{data} } = $i - 1; |
|
|
1
|
|
|
|
|
6
|
|
|
348
|
|
|
|
|
|
|
} |
|
349
|
|
|
|
|
|
|
sub DELETE { |
|
350
|
1
|
|
|
1
|
|
3
|
my ($self, $i) = @_; |
|
351
|
1
|
|
|
|
|
5
|
delete $self->{data}->[ $i ]; |
|
352
|
|
|
|
|
|
|
} |
|
353
|
|
|
|
|
|
|
sub EXISTS { |
|
354
|
2
|
|
|
2
|
|
6
|
my ($self, $i) = @_; |
|
355
|
2
|
|
|
|
|
14
|
return exists $self->{data}->[ $i ]; |
|
356
|
|
|
|
|
|
|
} |
|
357
|
|
|
|
|
|
|
sub CLEAR { |
|
358
|
1
|
|
|
1
|
|
6
|
my ($self) = @_; |
|
359
|
1
|
|
|
|
|
6
|
$self->{data} = []; |
|
360
|
|
|
|
|
|
|
} |
|
361
|
|
|
|
|
|
|
sub SHIFT { |
|
362
|
1
|
|
|
1
|
|
4
|
my ($self) = @_; |
|
363
|
1
|
|
|
|
|
4
|
shift @{ $self->{data} }; |
|
|
1
|
|
|
|
|
4
|
|
|
364
|
|
|
|
|
|
|
} |
|
365
|
|
|
|
|
|
|
sub UNSHIFT { |
|
366
|
2
|
|
|
2
|
|
9
|
my ($self, @args) = @_; |
|
367
|
2
|
|
|
|
|
3
|
unshift @{ $self->{data} }, map { _preserve $_ } @args; |
|
|
2
|
|
|
|
|
7
|
|
|
|
2
|
|
|
|
|
5
|
|
|
368
|
|
|
|
|
|
|
} |
|
369
|
|
|
|
|
|
|
sub SPLICE { |
|
370
|
2
|
|
|
2
|
|
12
|
my ($self, $offset, $length, @args) = @_; |
|
371
|
2
|
|
|
|
|
5
|
splice @{ $self->{data} }, $offset, $length, map { _preserve $_ } @args; |
|
|
2
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
7
|
|
|
372
|
|
|
|
|
|
|
} |
|
373
|
|
|
|
1
|
|
|
sub EXTEND {} |
|
374
|
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
package YAML::PP::Preserve::Scalar; |
|
377
|
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
use overload |
|
379
|
42
|
|
|
|
|
453
|
fallback => 1, |
|
380
|
|
|
|
|
|
|
'+' => \&value, |
|
381
|
|
|
|
|
|
|
'""' => \&value, |
|
382
|
|
|
|
|
|
|
'bool' => \&value, |
|
383
|
42
|
|
|
42
|
|
335
|
; |
|
|
42
|
|
|
|
|
60
|
|
|
384
|
|
|
|
|
|
|
sub new { |
|
385
|
87
|
|
|
87
|
|
187
|
my ($class, %args) = @_; |
|
386
|
87
|
|
|
|
|
239
|
my $self = { |
|
387
|
|
|
|
|
|
|
%args, |
|
388
|
|
|
|
|
|
|
}; |
|
389
|
87
|
|
|
|
|
294
|
bless $self, $class; |
|
390
|
|
|
|
|
|
|
} |
|
391
|
2218
|
|
|
2218
|
|
5924
|
sub value { $_[0]->{value} } |
|
392
|
0
|
|
|
0
|
|
0
|
sub tag { $_[0]->{tag} } |
|
393
|
21
|
100
|
|
21
|
|
58
|
sub style { $_[0]->{style} || 0 } |
|
394
|
17
|
|
|
17
|
|
42
|
sub alias { $_[0]->{alias} } |
|
395
|
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
1; |
|
397
|
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
__END__ |
|
399
|
|
|
|
|
|
|
|
|
400
|
|
|
|
|
|
|
=pod |
|
401
|
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
=encoding utf-8 |
|
403
|
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
=head1 NAME |
|
405
|
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
YAML::PP - YAML 1.2 processor |
|
407
|
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
409
|
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
WARNING: Most of the inner API is not stable yet. |
|
411
|
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
Here are a few examples of the basic load and dump methods: |
|
413
|
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
use YAML::PP; |
|
415
|
|
|
|
|
|
|
my $ypp = YAML::PP->new; |
|
416
|
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
my $yaml = <<'EOM'; |
|
418
|
|
|
|
|
|
|
--- # Document one is a mapping |
|
419
|
|
|
|
|
|
|
name: Tina |
|
420
|
|
|
|
|
|
|
age: 29 |
|
421
|
|
|
|
|
|
|
favourite language: Perl |
|
422
|
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
--- # Document two is a sequence |
|
424
|
|
|
|
|
|
|
- plain string |
|
425
|
|
|
|
|
|
|
- 'in single quotes' |
|
426
|
|
|
|
|
|
|
- "in double quotes we have escapes! like \t and \n" |
|
427
|
|
|
|
|
|
|
- | # a literal block scalar |
|
428
|
|
|
|
|
|
|
line1 |
|
429
|
|
|
|
|
|
|
line2 |
|
430
|
|
|
|
|
|
|
- > # a folded block scalar |
|
431
|
|
|
|
|
|
|
this is all one |
|
432
|
|
|
|
|
|
|
single line because the |
|
433
|
|
|
|
|
|
|
linebreaks will be folded |
|
434
|
|
|
|
|
|
|
EOM |
|
435
|
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
my @documents = $ypp->load_string($yaml); |
|
437
|
|
|
|
|
|
|
my @documents = $ypp->load_file($filename); |
|
438
|
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
my $yaml = $ypp->dump_string($data1, $data2); |
|
440
|
|
|
|
|
|
|
$ypp->dump_file($filename, $data1, $data2); |
|
441
|
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
# Enable perl data types and objects |
|
443
|
|
|
|
|
|
|
my $ypp = YAML::PP->new(schema => [qw/ + Perl /]); |
|
444
|
|
|
|
|
|
|
my $yaml = $yp->dump_string($data_with_perl_objects); |
|
445
|
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
# Legacy interface |
|
447
|
|
|
|
|
|
|
use YAML::PP qw/ Load Dump LoadFile DumpFile /; |
|
448
|
|
|
|
|
|
|
my @documents = Load($yaml); |
|
449
|
|
|
|
|
|
|
my @documents = LoadFile($filename); |
|
450
|
|
|
|
|
|
|
my @documents = LoadFile($filehandle); |
|
451
|
|
|
|
|
|
|
my $yaml = = Dump(@documents); |
|
452
|
|
|
|
|
|
|
DumpFile($filename, @documents); |
|
453
|
|
|
|
|
|
|
DumpFile($filenhandle @documents); |
|
454
|
|
|
|
|
|
|
|
|
455
|
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
Some utility scripts, mostly useful for debugging: |
|
457
|
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
# Load YAML into a data structure and dump with Data::Dumper |
|
459
|
|
|
|
|
|
|
yamlpp-load < file.yaml |
|
460
|
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
# Load and Dump |
|
462
|
|
|
|
|
|
|
yamlpp-load-dump < file.yaml |
|
463
|
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
# Print the events from the parser in yaml-test-suite format |
|
465
|
|
|
|
|
|
|
yamlpp-events < file.yaml |
|
466
|
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
# Parse and emit events directly without loading |
|
468
|
|
|
|
|
|
|
yamlpp-parse-emit < file.yaml |
|
469
|
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
# Create ANSI colored YAML. Can also be useful for invalid YAML, showing |
|
471
|
|
|
|
|
|
|
# you the exact location of the error |
|
472
|
|
|
|
|
|
|
yamlpp-highlight < file.yaml |
|
473
|
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
476
|
|
|
|
|
|
|
|
|
477
|
|
|
|
|
|
|
YAML::PP is a modular YAML processor. |
|
478
|
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
It aims to support C<YAML 1.2> and C<YAML 1.1>. See L<https://yaml.org/>. |
|
480
|
|
|
|
|
|
|
Some (rare) syntax elements are not yet supported and documented below. |
|
481
|
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
YAML is a serialization language. The YAML input is called "YAML Stream". |
|
483
|
|
|
|
|
|
|
A stream consists of one or more "Documents", separated by a line with a |
|
484
|
|
|
|
|
|
|
document start marker C<--->. A document optionally ends with the document |
|
485
|
|
|
|
|
|
|
end marker C<...>. |
|
486
|
|
|
|
|
|
|
|
|
487
|
|
|
|
|
|
|
This allows one to process continuous streams additionally to a fixed input |
|
488
|
|
|
|
|
|
|
file or string. |
|
489
|
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
The YAML::PP frontend will currently load all documents, and return only |
|
491
|
|
|
|
|
|
|
the first if called with scalar context. |
|
492
|
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
The YAML backend is implemented in a modular way that allows one to add |
|
494
|
|
|
|
|
|
|
custom handling of YAML tags, perl objects and data types. The inner API |
|
495
|
|
|
|
|
|
|
is not yet stable. Suggestions welcome. |
|
496
|
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
You can check out all current parse and load results from the |
|
498
|
|
|
|
|
|
|
yaml-test-suite here: |
|
499
|
|
|
|
|
|
|
L<https://perlpunk.github.io/YAML-PP-p5/test-suite.html> |
|
500
|
|
|
|
|
|
|
|
|
501
|
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
=head1 METHODS |
|
503
|
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
=head2 new |
|
505
|
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
my $ypp = YAML::PP->new; |
|
507
|
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
# use YAML 1.2 Failsafe Schema |
|
509
|
|
|
|
|
|
|
my $ypp = YAML::PP->new( schema => ['Failsafe'] ); |
|
510
|
|
|
|
|
|
|
# use YAML 1.2 JSON Schema |
|
511
|
|
|
|
|
|
|
my $ypp = YAML::PP->new( schema => ['JSON'] ); |
|
512
|
|
|
|
|
|
|
# use YAML 1.2 Core Schema |
|
513
|
|
|
|
|
|
|
my $ypp = YAML::PP->new( schema => ['Core'] ); |
|
514
|
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
# Die when detecting cyclic references |
|
516
|
|
|
|
|
|
|
my $ypp = YAML::PP->new( cyclic_refs => 'fatal' ); |
|
517
|
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
my $ypp = YAML::PP->new( |
|
519
|
|
|
|
|
|
|
boolean => 'perl', |
|
520
|
|
|
|
|
|
|
schema => ['Core'], |
|
521
|
|
|
|
|
|
|
cyclic_refs => 'fatal', |
|
522
|
|
|
|
|
|
|
indent => 4, |
|
523
|
|
|
|
|
|
|
header => 1, |
|
524
|
|
|
|
|
|
|
footer => 0, |
|
525
|
|
|
|
|
|
|
version_directive => 0, |
|
526
|
|
|
|
|
|
|
); |
|
527
|
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
Options: |
|
529
|
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
=over |
|
531
|
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
=item boolean |
|
533
|
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
Values: C<perl> (currently default), C<JSON::PP>, C<boolean>, C<perl_experimental> |
|
535
|
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
This option is for loading and dumping. |
|
537
|
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
In case of perl 5.36 and later, builtin booleans should work out of the box |
|
539
|
|
|
|
|
|
|
(since YAML::PP >= 0.38.0). |
|
540
|
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
print YAML::PP->new->dump_string([ builtin::true, !1 ]); |
|
542
|
|
|
|
|
|
|
# --- |
|
543
|
|
|
|
|
|
|
# - true |
|
544
|
|
|
|
|
|
|
# - false |
|
545
|
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
For earlier perl versions, you can use "pseudo" booleans like documented |
|
547
|
|
|
|
|
|
|
in the following examples. |
|
548
|
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
# load/dump booleans via boolean.pm |
|
550
|
|
|
|
|
|
|
my $ypp = YAML::PP->new( boolean => 'boolean' ); |
|
551
|
|
|
|
|
|
|
# load/dump booleans via JSON::PP::true/false |
|
552
|
|
|
|
|
|
|
my $ypp = YAML::PP->new( boolean => 'JSON::PP' ); |
|
553
|
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
You can also specify more than one class, comma separated. |
|
555
|
|
|
|
|
|
|
This is important for dumping. |
|
556
|
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
boolean => 'JSON::PP,boolean' |
|
559
|
|
|
|
|
|
|
Booleans will be loaded as JSON::PP::Booleans, but when dumping, also |
|
560
|
|
|
|
|
|
|
'boolean' objects will be recognized |
|
561
|
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
boolean => 'JSON::PP,*' |
|
563
|
|
|
|
|
|
|
Booleans will be loaded as JSON::PP::Booleans, but when dumping, all |
|
564
|
|
|
|
|
|
|
currently supported boolean classes will be recognized |
|
565
|
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
boolean => '*' |
|
567
|
|
|
|
|
|
|
Booleans will be loaded as perl booleans, but when dumping, all |
|
568
|
|
|
|
|
|
|
currently supported boolean classes will be recognized |
|
569
|
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
boolean => '' |
|
571
|
|
|
|
|
|
|
Booleans will be loaded as perl booleans, but when dumping, nothing |
|
572
|
|
|
|
|
|
|
will be recognized as booleans. |
|
573
|
|
|
|
|
|
|
This option is for backwards compatibility for perl versions < 5.36, |
|
574
|
|
|
|
|
|
|
if you rely on [!!1, !1] being dumped as [1, '']. |
|
575
|
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
The option C<perl_experimental> was introduced when experimental boolean |
|
577
|
|
|
|
|
|
|
support was added to perl 5.36. Since it will not be experimental anymore |
|
578
|
|
|
|
|
|
|
in perl 5.40 \o/ the option is deprecated and the same as C<perl>. |
|
579
|
|
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
=item schema |
|
581
|
|
|
|
|
|
|
|
|
582
|
|
|
|
|
|
|
Default: C<['Core']> |
|
583
|
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
This option is for loading and dumping. |
|
585
|
|
|
|
|
|
|
|
|
586
|
|
|
|
|
|
|
Array reference. Here you can define what schema to use. |
|
587
|
|
|
|
|
|
|
Supported standard Schemas are: C<Failsafe>, C<JSON>, C<Core>, C<YAML1_1>. |
|
588
|
|
|
|
|
|
|
|
|
589
|
|
|
|
|
|
|
To get an overview how the different Schemas behave, see |
|
590
|
|
|
|
|
|
|
L<https://perlpunk.github.io/YAML-PP-p5/schemas.html> |
|
591
|
|
|
|
|
|
|
|
|
592
|
|
|
|
|
|
|
Additionally you can add further schemas, for example C<Merge>. |
|
593
|
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
=item cyclic_refs |
|
595
|
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
Default: C<fatal> (since 0.037) |
|
597
|
|
|
|
|
|
|
|
|
598
|
|
|
|
|
|
|
Before the default was C<allow>, but this can lead to memory leaks |
|
599
|
|
|
|
|
|
|
when loading on untrusted data, so it was changed to C<fatal> by default. |
|
600
|
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
This option is for loading only. |
|
602
|
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
Defines what to do when a cyclic reference is detected when loading. |
|
604
|
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
# fatal - die |
|
606
|
|
|
|
|
|
|
# warn - Just warn about them and replace with undef |
|
607
|
|
|
|
|
|
|
# ignore - replace with undef |
|
608
|
|
|
|
|
|
|
# allow - Default |
|
609
|
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
=item duplicate_keys |
|
611
|
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
Default: 0 |
|
613
|
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
Since version 0.027 |
|
615
|
|
|
|
|
|
|
|
|
616
|
|
|
|
|
|
|
This option is for loading. |
|
617
|
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
The YAML Spec says duplicate mapping keys should be forbidden. |
|
619
|
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
When set to true, duplicate keys in mappings are allowed (and will overwrite |
|
621
|
|
|
|
|
|
|
the previous key). |
|
622
|
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
When set to false, duplicate keys will result in an error when loading. |
|
624
|
|
|
|
|
|
|
|
|
625
|
|
|
|
|
|
|
This is especially useful when you have a longer mapping and don't see |
|
626
|
|
|
|
|
|
|
the duplicate key in your editor: |
|
627
|
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
--- |
|
629
|
|
|
|
|
|
|
a: 1 |
|
630
|
|
|
|
|
|
|
b: 2 |
|
631
|
|
|
|
|
|
|
# ............. |
|
632
|
|
|
|
|
|
|
a: 23 # error |
|
633
|
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
=item indent |
|
635
|
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
Default: 2 |
|
637
|
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
This option is for dumping. |
|
639
|
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
Use that many spaces for indenting |
|
641
|
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
=item width |
|
643
|
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
Since version 0.025 |
|
645
|
|
|
|
|
|
|
|
|
646
|
|
|
|
|
|
|
Default: 80 |
|
647
|
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
This option is for dumping. |
|
649
|
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
Maximum columns when dumping. |
|
651
|
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
This is only respected when dumping flow collections right now. |
|
653
|
|
|
|
|
|
|
|
|
654
|
|
|
|
|
|
|
in the future it will be used also for wrapping long strings. |
|
655
|
|
|
|
|
|
|
|
|
656
|
|
|
|
|
|
|
=item header |
|
657
|
|
|
|
|
|
|
|
|
658
|
|
|
|
|
|
|
Default: 1 |
|
659
|
|
|
|
|
|
|
|
|
660
|
|
|
|
|
|
|
This option is for dumping. |
|
661
|
|
|
|
|
|
|
|
|
662
|
|
|
|
|
|
|
Print document header C<---> |
|
663
|
|
|
|
|
|
|
|
|
664
|
|
|
|
|
|
|
=item footer |
|
665
|
|
|
|
|
|
|
|
|
666
|
|
|
|
|
|
|
Default: 0 |
|
667
|
|
|
|
|
|
|
|
|
668
|
|
|
|
|
|
|
This option is for dumping. |
|
669
|
|
|
|
|
|
|
|
|
670
|
|
|
|
|
|
|
Print document footer C<...> |
|
671
|
|
|
|
|
|
|
|
|
672
|
|
|
|
|
|
|
=item require_footer |
|
673
|
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
Default: 0 |
|
675
|
|
|
|
|
|
|
|
|
676
|
|
|
|
|
|
|
Will require a C<...> at the end of each document. |
|
677
|
|
|
|
|
|
|
This can be useful in a context where you want to make sure you received |
|
678
|
|
|
|
|
|
|
the complete content, for example over network. |
|
679
|
|
|
|
|
|
|
|
|
680
|
|
|
|
|
|
|
# Good |
|
681
|
|
|
|
|
|
|
--- |
|
682
|
|
|
|
|
|
|
a: 1 |
|
683
|
|
|
|
|
|
|
... |
|
684
|
|
|
|
|
|
|
--- |
|
685
|
|
|
|
|
|
|
a: 2 |
|
686
|
|
|
|
|
|
|
... |
|
687
|
|
|
|
|
|
|
|
|
688
|
|
|
|
|
|
|
# Bad |
|
689
|
|
|
|
|
|
|
--- |
|
690
|
|
|
|
|
|
|
a: 1 |
|
691
|
|
|
|
|
|
|
--- |
|
692
|
|
|
|
|
|
|
a: 2 |
|
693
|
|
|
|
|
|
|
|
|
694
|
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
=item yaml_version |
|
696
|
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
Since version 0.020 |
|
698
|
|
|
|
|
|
|
|
|
699
|
|
|
|
|
|
|
This option is for loading and dumping. |
|
700
|
|
|
|
|
|
|
|
|
701
|
|
|
|
|
|
|
Default: C<1.2> |
|
702
|
|
|
|
|
|
|
|
|
703
|
|
|
|
|
|
|
Note that in this case, a directive C<%YAML 1.1> will basically be ignored |
|
704
|
|
|
|
|
|
|
and everything loaded with the C<1.2 Core> Schema. |
|
705
|
|
|
|
|
|
|
|
|
706
|
|
|
|
|
|
|
If you want to support both YAML 1.1 and 1.2, you have to specify that, and the |
|
707
|
|
|
|
|
|
|
schema (C<Core> or C<YAML1_1>) will be chosen automatically. |
|
708
|
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
my $yp = YAML::PP->new( |
|
710
|
|
|
|
|
|
|
yaml_version => ['1.2', '1.1'], |
|
711
|
|
|
|
|
|
|
); |
|
712
|
|
|
|
|
|
|
|
|
713
|
|
|
|
|
|
|
This is the same as |
|
714
|
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
my $yp = YAML::PP->new( |
|
716
|
|
|
|
|
|
|
schema => ['+'], |
|
717
|
|
|
|
|
|
|
yaml_version => ['1.2', '1.1'], |
|
718
|
|
|
|
|
|
|
); |
|
719
|
|
|
|
|
|
|
|
|
720
|
|
|
|
|
|
|
because the C<+> stands for the default schema per version. |
|
721
|
|
|
|
|
|
|
|
|
722
|
|
|
|
|
|
|
When loading, and there is no C<%YAML> directive, C<1.2> will be considered |
|
723
|
|
|
|
|
|
|
as default, and the C<Core> schema will be used. |
|
724
|
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
If there is a C<%YAML 1.1> directive, the C<YAML1_1> schema will be used. |
|
726
|
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
Of course, you can also make C<1.1> the default: |
|
728
|
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
my $yp = YAML::PP->new( |
|
730
|
|
|
|
|
|
|
yaml_version => ['1.1', '1.2'], |
|
731
|
|
|
|
|
|
|
); |
|
732
|
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
|
|
734
|
|
|
|
|
|
|
You can also specify C<1.1> only: |
|
735
|
|
|
|
|
|
|
|
|
736
|
|
|
|
|
|
|
my $yp = YAML::PP->new( |
|
737
|
|
|
|
|
|
|
yaml_version => ['1.1'], |
|
738
|
|
|
|
|
|
|
); |
|
739
|
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
In this case also documents with C<%YAML 1.2> will be loaded with the C<YAML1_1> |
|
741
|
|
|
|
|
|
|
schema. |
|
742
|
|
|
|
|
|
|
|
|
743
|
|
|
|
|
|
|
=item version_directive |
|
744
|
|
|
|
|
|
|
|
|
745
|
|
|
|
|
|
|
Since version 0.020 |
|
746
|
|
|
|
|
|
|
|
|
747
|
|
|
|
|
|
|
This option is for dumping. |
|
748
|
|
|
|
|
|
|
|
|
749
|
|
|
|
|
|
|
Default: 0 |
|
750
|
|
|
|
|
|
|
|
|
751
|
|
|
|
|
|
|
Print Version Directive C<%YAML 1.2> (or C<%YAML 1.1>) on top of each YAML |
|
752
|
|
|
|
|
|
|
document. It will use the first version specified in the C<yaml_version> option. |
|
753
|
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
=item preserve |
|
755
|
|
|
|
|
|
|
|
|
756
|
|
|
|
|
|
|
Since version 0.021 |
|
757
|
|
|
|
|
|
|
|
|
758
|
|
|
|
|
|
|
Default: false |
|
759
|
|
|
|
|
|
|
|
|
760
|
|
|
|
|
|
|
This option is for loading and dumping. |
|
761
|
|
|
|
|
|
|
|
|
762
|
|
|
|
|
|
|
Preserving scalar styles is still experimental. |
|
763
|
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
use YAML::PP::Common qw/ :PRESERVE /; |
|
765
|
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
# Preserve the order of hash keys |
|
767
|
|
|
|
|
|
|
my $yp = YAML::PP->new( preserve => PRESERVE_ORDER ); |
|
768
|
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
# Preserve the quoting style of scalars |
|
770
|
|
|
|
|
|
|
my $yp = YAML::PP->new( preserve => PRESERVE_SCALAR_STYLE ); |
|
771
|
|
|
|
|
|
|
|
|
772
|
|
|
|
|
|
|
# Preserve block/flow style (since 0.024) |
|
773
|
|
|
|
|
|
|
my $yp = YAML::PP->new( preserve => PRESERVE_FLOW_STYLE ); |
|
774
|
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
# Preserve alias names (since 0.027) |
|
776
|
|
|
|
|
|
|
my $yp = YAML::PP->new( preserve => PRESERVE_ALIAS ); |
|
777
|
|
|
|
|
|
|
|
|
778
|
|
|
|
|
|
|
# Combine, e.g. preserve order and scalar style |
|
779
|
|
|
|
|
|
|
my $yp = YAML::PP->new( preserve => PRESERVE_ORDER | PRESERVE_SCALAR_STYLE ); |
|
780
|
|
|
|
|
|
|
|
|
781
|
|
|
|
|
|
|
Do NOT rely on the internal implementation of it. |
|
782
|
|
|
|
|
|
|
|
|
783
|
|
|
|
|
|
|
If you load the following input: |
|
784
|
|
|
|
|
|
|
|
|
785
|
|
|
|
|
|
|
--- |
|
786
|
|
|
|
|
|
|
z: 1 |
|
787
|
|
|
|
|
|
|
a: 2 |
|
788
|
|
|
|
|
|
|
--- |
|
789
|
|
|
|
|
|
|
- plain |
|
790
|
|
|
|
|
|
|
- 'single' |
|
791
|
|
|
|
|
|
|
- "double" |
|
792
|
|
|
|
|
|
|
- | |
|
793
|
|
|
|
|
|
|
literal |
|
794
|
|
|
|
|
|
|
- > |
|
795
|
|
|
|
|
|
|
folded |
|
796
|
|
|
|
|
|
|
--- |
|
797
|
|
|
|
|
|
|
block mapping: &alias |
|
798
|
|
|
|
|
|
|
flow sequence: [a, b] |
|
799
|
|
|
|
|
|
|
same mapping: *alias |
|
800
|
|
|
|
|
|
|
flow mapping: {a: b} |
|
801
|
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
with this code: |
|
804
|
|
|
|
|
|
|
|
|
805
|
|
|
|
|
|
|
my $yp = YAML::PP->new( |
|
806
|
|
|
|
|
|
|
preserve => PRESERVE_ORDER | PRESERVE_SCALAR_STYLE |
|
807
|
|
|
|
|
|
|
| PRESERVE_FLOW_STYLE | PRESERVE_ALIAS |
|
808
|
|
|
|
|
|
|
); |
|
809
|
|
|
|
|
|
|
my ($hash, $styles, $flow) = $yp->load_file($file); |
|
810
|
|
|
|
|
|
|
$yp->dump_file($hash, $styles, $flow); |
|
811
|
|
|
|
|
|
|
|
|
812
|
|
|
|
|
|
|
Then dumping it will return the same output. |
|
813
|
|
|
|
|
|
|
|
|
814
|
|
|
|
|
|
|
Note that YAML allows repeated definition of anchors. They cannot be preserved |
|
815
|
|
|
|
|
|
|
with YAML::PP right now. Example: |
|
816
|
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
--- |
|
818
|
|
|
|
|
|
|
- &seq [a] |
|
819
|
|
|
|
|
|
|
- *seq |
|
820
|
|
|
|
|
|
|
- &seq [b] |
|
821
|
|
|
|
|
|
|
- *seq |
|
822
|
|
|
|
|
|
|
|
|
823
|
|
|
|
|
|
|
Because the data could be shuffled before dumping again, the anchor definition |
|
824
|
|
|
|
|
|
|
could be broken. In this case repeated anchor names will be discarded when |
|
825
|
|
|
|
|
|
|
loading and dumped with numeric anchors like usual. |
|
826
|
|
|
|
|
|
|
|
|
827
|
|
|
|
|
|
|
Implementation: |
|
828
|
|
|
|
|
|
|
|
|
829
|
|
|
|
|
|
|
When loading, hashes will be tied to an internal class |
|
830
|
|
|
|
|
|
|
(C<YAML::PP::Preserve::Hash>) that keeps the key order. |
|
831
|
|
|
|
|
|
|
|
|
832
|
|
|
|
|
|
|
Scalars will be returned as objects of an internal class |
|
833
|
|
|
|
|
|
|
(C<YAML::PP::Preserve::Scalar>) with overloading. If you assign to such |
|
834
|
|
|
|
|
|
|
a scalar, the object will be replaced by a simple scalar. |
|
835
|
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
# assignment, style gets lost |
|
837
|
|
|
|
|
|
|
$styles->[1] .= ' append'; |
|
838
|
|
|
|
|
|
|
|
|
839
|
|
|
|
|
|
|
You can also pass C<1> as a value. In this case all preserving options will be |
|
840
|
|
|
|
|
|
|
enabled, also if there are new options added in the future. |
|
841
|
|
|
|
|
|
|
|
|
842
|
|
|
|
|
|
|
There are also methods to create preserved nodes from scratch. See the |
|
843
|
|
|
|
|
|
|
C<preserved_(scalar|mapping|sequence)> L<"METHODS"> below. |
|
844
|
|
|
|
|
|
|
|
|
845
|
|
|
|
|
|
|
=back |
|
846
|
|
|
|
|
|
|
|
|
847
|
|
|
|
|
|
|
=head2 load_string |
|
848
|
|
|
|
|
|
|
|
|
849
|
|
|
|
|
|
|
my $doc = $ypp->load_string("foo: bar"); |
|
850
|
|
|
|
|
|
|
my @docs = $ypp->load_string("foo: bar\n---\n- a"); |
|
851
|
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
Input should be Unicode characters. |
|
853
|
|
|
|
|
|
|
|
|
854
|
|
|
|
|
|
|
So if you read from a file, you should decode it, for example with |
|
855
|
|
|
|
|
|
|
C<Encode::decode()>. |
|
856
|
|
|
|
|
|
|
|
|
857
|
|
|
|
|
|
|
Note that in scalar context, C<load_string> and C<load_file> return the first |
|
858
|
|
|
|
|
|
|
document (like L<YAML::Syck>), while L<YAML> and L<YAML::XS> return the |
|
859
|
|
|
|
|
|
|
last. |
|
860
|
|
|
|
|
|
|
|
|
861
|
|
|
|
|
|
|
=head2 load_file |
|
862
|
|
|
|
|
|
|
|
|
863
|
|
|
|
|
|
|
my $doc = $ypp->load_file("file.yaml"); |
|
864
|
|
|
|
|
|
|
my @docs = $ypp->load_file("file.yaml"); |
|
865
|
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
Strings will be loaded as unicode characters. |
|
867
|
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
=head2 dump_string |
|
869
|
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
my $yaml = $ypp->dump_string($doc); |
|
871
|
|
|
|
|
|
|
my $yaml = $ypp->dump_string($doc1, $doc2); |
|
872
|
|
|
|
|
|
|
my $yaml = $ypp->dump_string(@docs); |
|
873
|
|
|
|
|
|
|
|
|
874
|
|
|
|
|
|
|
Input strings should be Unicode characters. |
|
875
|
|
|
|
|
|
|
|
|
876
|
|
|
|
|
|
|
Output will return Unicode characters. |
|
877
|
|
|
|
|
|
|
|
|
878
|
|
|
|
|
|
|
So if you want to write that to a file (or pass to YAML::XS, for example), |
|
879
|
|
|
|
|
|
|
you typically encode it via C<Encode::encode()>. |
|
880
|
|
|
|
|
|
|
|
|
881
|
|
|
|
|
|
|
=head2 dump_file |
|
882
|
|
|
|
|
|
|
|
|
883
|
|
|
|
|
|
|
$ypp->dump_file("file.yaml", $doc); |
|
884
|
|
|
|
|
|
|
$ypp->dump_file("file.yaml", $doc1, $doc2); |
|
885
|
|
|
|
|
|
|
$ypp->dump_file("file.yaml", @docs); |
|
886
|
|
|
|
|
|
|
|
|
887
|
|
|
|
|
|
|
Input data should be Unicode characters. |
|
888
|
|
|
|
|
|
|
|
|
889
|
|
|
|
|
|
|
=head2 dump |
|
890
|
|
|
|
|
|
|
|
|
891
|
|
|
|
|
|
|
This will dump to a predefined writer. By default it will just use the |
|
892
|
|
|
|
|
|
|
L<YAML::PP::Writer> and output a string. |
|
893
|
|
|
|
|
|
|
|
|
894
|
|
|
|
|
|
|
my $writer = MyWriter->new(\my $output); |
|
895
|
|
|
|
|
|
|
my $yp = YAML::PP->new( |
|
896
|
|
|
|
|
|
|
writer => $writer, |
|
897
|
|
|
|
|
|
|
); |
|
898
|
|
|
|
|
|
|
$yp->dump($data); |
|
899
|
|
|
|
|
|
|
|
|
900
|
|
|
|
|
|
|
=head2 preserved_scalar |
|
901
|
|
|
|
|
|
|
|
|
902
|
|
|
|
|
|
|
Since version 0.024 |
|
903
|
|
|
|
|
|
|
|
|
904
|
|
|
|
|
|
|
Experimental. Please report bugs or let me know this is useful and works. |
|
905
|
|
|
|
|
|
|
|
|
906
|
|
|
|
|
|
|
You can define a certain scalar style when dumping data. |
|
907
|
|
|
|
|
|
|
Figuring out the best style is a hard task and practically impossible to get |
|
908
|
|
|
|
|
|
|
it right for all cases. It's also a matter of taste. |
|
909
|
|
|
|
|
|
|
|
|
910
|
|
|
|
|
|
|
use YAML::PP::Common qw/ PRESERVE_SCALAR_STYLE YAML_LITERAL_SCALAR_STYLE /; |
|
911
|
|
|
|
|
|
|
my $yp = YAML::PP->new( |
|
912
|
|
|
|
|
|
|
preserve => PRESERVE_SCALAR_STYLE, |
|
913
|
|
|
|
|
|
|
); |
|
914
|
|
|
|
|
|
|
# a single linebreak would normally be dumped with double quotes: "\n" |
|
915
|
|
|
|
|
|
|
my $scalar = $yp->preserved_scalar("\n", style => YAML_LITERAL_SCALAR_STYLE ); |
|
916
|
|
|
|
|
|
|
|
|
917
|
|
|
|
|
|
|
my $data = { literal => $scalar }; |
|
918
|
|
|
|
|
|
|
my $dump = $yp->dump_string($data); |
|
919
|
|
|
|
|
|
|
# output |
|
920
|
|
|
|
|
|
|
--- |
|
921
|
|
|
|
|
|
|
literal: |+ |
|
922
|
|
|
|
|
|
|
|
|
923
|
|
|
|
|
|
|
... |
|
924
|
|
|
|
|
|
|
|
|
925
|
|
|
|
|
|
|
|
|
926
|
|
|
|
|
|
|
=head2 preserved_mapping, preserved_sequence |
|
927
|
|
|
|
|
|
|
|
|
928
|
|
|
|
|
|
|
Since version 0.024 |
|
929
|
|
|
|
|
|
|
|
|
930
|
|
|
|
|
|
|
Experimental. Please report bugs or let me know this is useful and works. |
|
931
|
|
|
|
|
|
|
|
|
932
|
|
|
|
|
|
|
With this you can define which nodes are dumped with the more compact flow |
|
933
|
|
|
|
|
|
|
style instead of block style. |
|
934
|
|
|
|
|
|
|
|
|
935
|
|
|
|
|
|
|
If you add C<PRESERVE_ORDER> to the C<preserve> option, it will also keep the |
|
936
|
|
|
|
|
|
|
order of the keys in a hash. |
|
937
|
|
|
|
|
|
|
|
|
938
|
|
|
|
|
|
|
use YAML::PP::Common qw/ |
|
939
|
|
|
|
|
|
|
PRESERVE_ORDER PRESERVE_FLOW_STYLE |
|
940
|
|
|
|
|
|
|
YAML_FLOW_MAPPING_STYLE YAML_FLOW_SEQUENCE_STYLE |
|
941
|
|
|
|
|
|
|
/; |
|
942
|
|
|
|
|
|
|
my $yp = YAML::PP->new( |
|
943
|
|
|
|
|
|
|
preserve => PRESERVE_FLOW_STYLE | PRESERVE_ORDER |
|
944
|
|
|
|
|
|
|
); |
|
945
|
|
|
|
|
|
|
|
|
946
|
|
|
|
|
|
|
my $hash = $yp->preserved_mapping({}, style => YAML_FLOW_MAPPING_STYLE); |
|
947
|
|
|
|
|
|
|
# Add values after initialization to preserve order |
|
948
|
|
|
|
|
|
|
%$hash = (z => 1, a => 2, y => 3, b => 4); |
|
949
|
|
|
|
|
|
|
|
|
950
|
|
|
|
|
|
|
my $array = $yp->preserved_sequence([23, 24], style => YAML_FLOW_SEQUENCE_STYLE); |
|
951
|
|
|
|
|
|
|
|
|
952
|
|
|
|
|
|
|
my $data = $yp->preserved_mapping({}); |
|
953
|
|
|
|
|
|
|
%$data = ( map => $hash, seq => $array ); |
|
954
|
|
|
|
|
|
|
|
|
955
|
|
|
|
|
|
|
my $dump = $yp->dump_string($data); |
|
956
|
|
|
|
|
|
|
# output |
|
957
|
|
|
|
|
|
|
--- |
|
958
|
|
|
|
|
|
|
map: {z: 1, a: 2, y: 3, b: 4} |
|
959
|
|
|
|
|
|
|
seq: [23, 24] |
|
960
|
|
|
|
|
|
|
|
|
961
|
|
|
|
|
|
|
|
|
962
|
|
|
|
|
|
|
=head2 loader |
|
963
|
|
|
|
|
|
|
|
|
964
|
|
|
|
|
|
|
Returns or sets the loader object, by default L<YAML::PP::Loader> |
|
965
|
|
|
|
|
|
|
|
|
966
|
|
|
|
|
|
|
=head2 dumper |
|
967
|
|
|
|
|
|
|
|
|
968
|
|
|
|
|
|
|
Returns or sets the dumper object, by default L<YAML::PP::Dumper> |
|
969
|
|
|
|
|
|
|
|
|
970
|
|
|
|
|
|
|
=head2 schema |
|
971
|
|
|
|
|
|
|
|
|
972
|
|
|
|
|
|
|
Returns or sets the schema object |
|
973
|
|
|
|
|
|
|
|
|
974
|
|
|
|
|
|
|
=head2 default_schema |
|
975
|
|
|
|
|
|
|
|
|
976
|
|
|
|
|
|
|
Creates and returns the default schema |
|
977
|
|
|
|
|
|
|
|
|
978
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
979
|
|
|
|
|
|
|
|
|
980
|
|
|
|
|
|
|
The functions C<Load>, C<LoadFile>, C<Dump> and C<DumpFile> are provided |
|
981
|
|
|
|
|
|
|
as a drop-in replacement for other existing YAML processors. |
|
982
|
|
|
|
|
|
|
No function is exported by default. |
|
983
|
|
|
|
|
|
|
|
|
984
|
|
|
|
|
|
|
Note that in scalar context, C<Load> and C<LoadFile> return the first |
|
985
|
|
|
|
|
|
|
document (like L<YAML::Syck>), while L<YAML> and L<YAML::XS> return the |
|
986
|
|
|
|
|
|
|
last. |
|
987
|
|
|
|
|
|
|
|
|
988
|
|
|
|
|
|
|
=over |
|
989
|
|
|
|
|
|
|
|
|
990
|
|
|
|
|
|
|
=item Load |
|
991
|
|
|
|
|
|
|
|
|
992
|
|
|
|
|
|
|
use YAML::PP qw/ Load /; |
|
993
|
|
|
|
|
|
|
my $doc = Load($yaml); |
|
994
|
|
|
|
|
|
|
my @docs = Load($yaml); |
|
995
|
|
|
|
|
|
|
|
|
996
|
|
|
|
|
|
|
Works like C<load_string>. |
|
997
|
|
|
|
|
|
|
|
|
998
|
|
|
|
|
|
|
=item LoadFile |
|
999
|
|
|
|
|
|
|
|
|
1000
|
|
|
|
|
|
|
use YAML::PP qw/ LoadFile /; |
|
1001
|
|
|
|
|
|
|
my $doc = LoadFile($file); |
|
1002
|
|
|
|
|
|
|
my @docs = LoadFile($file); |
|
1003
|
|
|
|
|
|
|
my @docs = LoadFile($filehandle); |
|
1004
|
|
|
|
|
|
|
|
|
1005
|
|
|
|
|
|
|
Works like C<load_file>. |
|
1006
|
|
|
|
|
|
|
|
|
1007
|
|
|
|
|
|
|
=item Dump |
|
1008
|
|
|
|
|
|
|
|
|
1009
|
|
|
|
|
|
|
use YAML::PP qw/ Dump /; |
|
1010
|
|
|
|
|
|
|
my $yaml = Dump($doc); |
|
1011
|
|
|
|
|
|
|
my $yaml = Dump(@docs); |
|
1012
|
|
|
|
|
|
|
|
|
1013
|
|
|
|
|
|
|
Works like C<dump_string>. |
|
1014
|
|
|
|
|
|
|
|
|
1015
|
|
|
|
|
|
|
=item DumpFile |
|
1016
|
|
|
|
|
|
|
|
|
1017
|
|
|
|
|
|
|
use YAML::PP qw/ DumpFile /; |
|
1018
|
|
|
|
|
|
|
DumpFile($file, $doc); |
|
1019
|
|
|
|
|
|
|
DumpFile($file, @docs); |
|
1020
|
|
|
|
|
|
|
DumpFile($filehandle, @docs); |
|
1021
|
|
|
|
|
|
|
|
|
1022
|
|
|
|
|
|
|
Works like C<dump_file>. |
|
1023
|
|
|
|
|
|
|
|
|
1024
|
|
|
|
|
|
|
=back |
|
1025
|
|
|
|
|
|
|
|
|
1026
|
|
|
|
|
|
|
=head1 PLUGINS |
|
1027
|
|
|
|
|
|
|
|
|
1028
|
|
|
|
|
|
|
You can alter the behaviour of YAML::PP by using the following schema |
|
1029
|
|
|
|
|
|
|
classes: |
|
1030
|
|
|
|
|
|
|
|
|
1031
|
|
|
|
|
|
|
=over |
|
1032
|
|
|
|
|
|
|
|
|
1033
|
|
|
|
|
|
|
=item L<YAML::PP::Schema::Failsafe> |
|
1034
|
|
|
|
|
|
|
|
|
1035
|
|
|
|
|
|
|
One of the three YAML 1.2 official schemas |
|
1036
|
|
|
|
|
|
|
|
|
1037
|
|
|
|
|
|
|
=item L<YAML::PP::Schema::JSON> |
|
1038
|
|
|
|
|
|
|
|
|
1039
|
|
|
|
|
|
|
One of the three YAML 1.2 official schemas. |
|
1040
|
|
|
|
|
|
|
|
|
1041
|
|
|
|
|
|
|
=item L<YAML::PP::Schema::Core> |
|
1042
|
|
|
|
|
|
|
|
|
1043
|
|
|
|
|
|
|
One of the three YAML 1.2 official schemas. Default |
|
1044
|
|
|
|
|
|
|
|
|
1045
|
|
|
|
|
|
|
=item L<YAML::PP::Schema::YAML1_1> |
|
1046
|
|
|
|
|
|
|
|
|
1047
|
|
|
|
|
|
|
Schema implementing the most common YAML 1.1 types |
|
1048
|
|
|
|
|
|
|
|
|
1049
|
|
|
|
|
|
|
=item L<YAML::PP::Schema::Perl> |
|
1050
|
|
|
|
|
|
|
|
|
1051
|
|
|
|
|
|
|
Serializing Perl objects and types |
|
1052
|
|
|
|
|
|
|
|
|
1053
|
|
|
|
|
|
|
=item L<YAML::PP::Schema::Binary> |
|
1054
|
|
|
|
|
|
|
|
|
1055
|
|
|
|
|
|
|
Serializing binary data |
|
1056
|
|
|
|
|
|
|
|
|
1057
|
|
|
|
|
|
|
=item L<YAML::PP::Schema::Merge> |
|
1058
|
|
|
|
|
|
|
|
|
1059
|
|
|
|
|
|
|
YAML 1.1 merge keys for mappings |
|
1060
|
|
|
|
|
|
|
|
|
1061
|
|
|
|
|
|
|
=item L<YAML::PP::Schema::Catchall> |
|
1062
|
|
|
|
|
|
|
|
|
1063
|
|
|
|
|
|
|
Experimental. |
|
1064
|
|
|
|
|
|
|
|
|
1065
|
|
|
|
|
|
|
It was accidentally added in 0.38.1, and unknown tags forbidden. |
|
1066
|
|
|
|
|
|
|
This was reverted in 0.39.0 |
|
1067
|
|
|
|
|
|
|
|
|
1068
|
|
|
|
|
|
|
By default they will result in an error. |
|
1069
|
|
|
|
|
|
|
|
|
1070
|
|
|
|
|
|
|
=item L<YAML::PP::Schema::Include> |
|
1071
|
|
|
|
|
|
|
|
|
1072
|
|
|
|
|
|
|
Include other YAML files via C<!include> tags |
|
1073
|
|
|
|
|
|
|
|
|
1074
|
|
|
|
|
|
|
=item L<YAML::PP::Schema::Tie::IxHash> |
|
1075
|
|
|
|
|
|
|
|
|
1076
|
|
|
|
|
|
|
Deprecated. See option C<preserve> |
|
1077
|
|
|
|
|
|
|
|
|
1078
|
|
|
|
|
|
|
=back |
|
1079
|
|
|
|
|
|
|
|
|
1080
|
|
|
|
|
|
|
To make the parsing process faster, you can plugin the libyaml parser |
|
1081
|
|
|
|
|
|
|
with L<YAML::PP::LibYAML>. |
|
1082
|
|
|
|
|
|
|
|
|
1083
|
|
|
|
|
|
|
|
|
1084
|
|
|
|
|
|
|
|
|
1085
|
|
|
|
|
|
|
=head1 IMPLEMENTATION |
|
1086
|
|
|
|
|
|
|
|
|
1087
|
|
|
|
|
|
|
The process of loading and dumping is split into the following steps: |
|
1088
|
|
|
|
|
|
|
|
|
1089
|
|
|
|
|
|
|
Load: |
|
1090
|
|
|
|
|
|
|
|
|
1091
|
|
|
|
|
|
|
YAML Stream Tokens Event List Data Structure |
|
1092
|
|
|
|
|
|
|
---------> ---------> ---------> |
|
1093
|
|
|
|
|
|
|
lex parse construct |
|
1094
|
|
|
|
|
|
|
|
|
1095
|
|
|
|
|
|
|
|
|
1096
|
|
|
|
|
|
|
Dump: |
|
1097
|
|
|
|
|
|
|
|
|
1098
|
|
|
|
|
|
|
Data Structure Event List YAML Stream |
|
1099
|
|
|
|
|
|
|
---------> ---------> |
|
1100
|
|
|
|
|
|
|
represent emit |
|
1101
|
|
|
|
|
|
|
|
|
1102
|
|
|
|
|
|
|
|
|
1103
|
|
|
|
|
|
|
You can dump basic perl types like hashes, arrays, scalars (strings, numbers). |
|
1104
|
|
|
|
|
|
|
For dumping blessed objects and things like coderefs have a look at |
|
1105
|
|
|
|
|
|
|
L<YAML::PP::Perl>/L<YAML::PP::Schema::Perl>. |
|
1106
|
|
|
|
|
|
|
|
|
1107
|
|
|
|
|
|
|
=over |
|
1108
|
|
|
|
|
|
|
|
|
1109
|
|
|
|
|
|
|
=item L<YAML::PP::Lexer> |
|
1110
|
|
|
|
|
|
|
|
|
1111
|
|
|
|
|
|
|
The Lexer is reading the YAML stream into tokens. This makes it possible |
|
1112
|
|
|
|
|
|
|
to generate syntax highlighted YAML output. |
|
1113
|
|
|
|
|
|
|
|
|
1114
|
|
|
|
|
|
|
Note that the API to retrieve the tokens will change. |
|
1115
|
|
|
|
|
|
|
|
|
1116
|
|
|
|
|
|
|
=item L<YAML::PP::Parser> |
|
1117
|
|
|
|
|
|
|
|
|
1118
|
|
|
|
|
|
|
The Parser retrieves the tokens from the Lexer. The main YAML content is then |
|
1119
|
|
|
|
|
|
|
parsed with the Grammar. |
|
1120
|
|
|
|
|
|
|
|
|
1121
|
|
|
|
|
|
|
=item L<YAML::PP::Grammar> |
|
1122
|
|
|
|
|
|
|
|
|
1123
|
|
|
|
|
|
|
=item L<YAML::PP::Constructor> |
|
1124
|
|
|
|
|
|
|
|
|
1125
|
|
|
|
|
|
|
The Constructor creates a data structure from the Parser events. |
|
1126
|
|
|
|
|
|
|
|
|
1127
|
|
|
|
|
|
|
=item L<YAML::PP::Loader> |
|
1128
|
|
|
|
|
|
|
|
|
1129
|
|
|
|
|
|
|
The Loader combines the constructor and parser. |
|
1130
|
|
|
|
|
|
|
|
|
1131
|
|
|
|
|
|
|
=item L<YAML::PP::Dumper> |
|
1132
|
|
|
|
|
|
|
|
|
1133
|
|
|
|
|
|
|
The Dumper will delegate to the Representer |
|
1134
|
|
|
|
|
|
|
|
|
1135
|
|
|
|
|
|
|
=item L<YAML::PP::Representer> |
|
1136
|
|
|
|
|
|
|
|
|
1137
|
|
|
|
|
|
|
The Representer will create Emitter events from the given data structure. |
|
1138
|
|
|
|
|
|
|
|
|
1139
|
|
|
|
|
|
|
=item L<YAML::PP::Emitter> |
|
1140
|
|
|
|
|
|
|
|
|
1141
|
|
|
|
|
|
|
The Emitter creates a YAML stream. |
|
1142
|
|
|
|
|
|
|
|
|
1143
|
|
|
|
|
|
|
=back |
|
1144
|
|
|
|
|
|
|
|
|
1145
|
|
|
|
|
|
|
=head2 YAML::PP::Parser |
|
1146
|
|
|
|
|
|
|
|
|
1147
|
|
|
|
|
|
|
Still TODO: |
|
1148
|
|
|
|
|
|
|
|
|
1149
|
|
|
|
|
|
|
=over 4 |
|
1150
|
|
|
|
|
|
|
|
|
1151
|
|
|
|
|
|
|
=item Implicit collection keys |
|
1152
|
|
|
|
|
|
|
|
|
1153
|
|
|
|
|
|
|
--- |
|
1154
|
|
|
|
|
|
|
[ a, b, c ]: value |
|
1155
|
|
|
|
|
|
|
|
|
1156
|
|
|
|
|
|
|
=item Implicit mapping in flow style sequences |
|
1157
|
|
|
|
|
|
|
|
|
1158
|
|
|
|
|
|
|
This is supported since 0.029 (except some not relevant cases): |
|
1159
|
|
|
|
|
|
|
|
|
1160
|
|
|
|
|
|
|
--- |
|
1161
|
|
|
|
|
|
|
[ a, b, c: d ] |
|
1162
|
|
|
|
|
|
|
# equals |
|
1163
|
|
|
|
|
|
|
[ a, b, { c: d } ] |
|
1164
|
|
|
|
|
|
|
|
|
1165
|
|
|
|
|
|
|
=item Plain mapping keys ending with colons |
|
1166
|
|
|
|
|
|
|
|
|
1167
|
|
|
|
|
|
|
--- |
|
1168
|
|
|
|
|
|
|
key ends with two colons::: value |
|
1169
|
|
|
|
|
|
|
|
|
1170
|
|
|
|
|
|
|
This was implemented in 0.037. |
|
1171
|
|
|
|
|
|
|
|
|
1172
|
|
|
|
|
|
|
=item Supported Characters |
|
1173
|
|
|
|
|
|
|
|
|
1174
|
|
|
|
|
|
|
If you have valid YAML that's not parsed, or the other way round, please |
|
1175
|
|
|
|
|
|
|
create an issue. |
|
1176
|
|
|
|
|
|
|
|
|
1177
|
|
|
|
|
|
|
=item Line and Column Numbers |
|
1178
|
|
|
|
|
|
|
|
|
1179
|
|
|
|
|
|
|
You will see line and column numbers in the error message. The column numbers |
|
1180
|
|
|
|
|
|
|
might still be wrong in some cases. |
|
1181
|
|
|
|
|
|
|
|
|
1182
|
|
|
|
|
|
|
=item Error Messages |
|
1183
|
|
|
|
|
|
|
|
|
1184
|
|
|
|
|
|
|
The error messages need to be improved. |
|
1185
|
|
|
|
|
|
|
|
|
1186
|
|
|
|
|
|
|
=item Unicode Surrogate Pairs |
|
1187
|
|
|
|
|
|
|
|
|
1188
|
|
|
|
|
|
|
Currently loaded as single characters without validating |
|
1189
|
|
|
|
|
|
|
|
|
1190
|
|
|
|
|
|
|
=item Possibly more |
|
1191
|
|
|
|
|
|
|
|
|
1192
|
|
|
|
|
|
|
=back |
|
1193
|
|
|
|
|
|
|
|
|
1194
|
|
|
|
|
|
|
=head2 YAML::PP::Constructor |
|
1195
|
|
|
|
|
|
|
|
|
1196
|
|
|
|
|
|
|
The Constructor now supports all three YAML 1.2 Schemas, Failsafe, JSON and |
|
1197
|
|
|
|
|
|
|
Core. Additionally you can choose the schema for YAML 1.1 as C<YAML1_1>. |
|
1198
|
|
|
|
|
|
|
|
|
1199
|
|
|
|
|
|
|
Too see what strings are resolved as booleans, numbers, null etc. look at |
|
1200
|
|
|
|
|
|
|
L<https://perlpunk.github.io/YAML-PP-p5/schema-examples.html>. |
|
1201
|
|
|
|
|
|
|
|
|
1202
|
|
|
|
|
|
|
You can choose the Schema like this: |
|
1203
|
|
|
|
|
|
|
|
|
1204
|
|
|
|
|
|
|
my $ypp = YAML::PP->new(schema => ['JSON']); # default is 'Core' |
|
1205
|
|
|
|
|
|
|
|
|
1206
|
|
|
|
|
|
|
The Tags C<!!seq> and C<!!map> are still ignored for now. |
|
1207
|
|
|
|
|
|
|
|
|
1208
|
|
|
|
|
|
|
It supports: |
|
1209
|
|
|
|
|
|
|
|
|
1210
|
|
|
|
|
|
|
=over 4 |
|
1211
|
|
|
|
|
|
|
|
|
1212
|
|
|
|
|
|
|
=item Handling of Anchors/Aliases |
|
1213
|
|
|
|
|
|
|
|
|
1214
|
|
|
|
|
|
|
Like in modules like L<YAML>, the Constructor will use references for mappings and |
|
1215
|
|
|
|
|
|
|
sequences, but obviously not for scalars. |
|
1216
|
|
|
|
|
|
|
|
|
1217
|
|
|
|
|
|
|
L<YAML::XS> uses real aliases, which allows also aliasing scalars. I might add |
|
1218
|
|
|
|
|
|
|
an option for that since aliasing is now available in pure perl. |
|
1219
|
|
|
|
|
|
|
|
|
1220
|
|
|
|
|
|
|
=item Boolean Handling |
|
1221
|
|
|
|
|
|
|
|
|
1222
|
|
|
|
|
|
|
You can choose between C<'perl'> (1/'', currently default), C<'JSON::PP'> and |
|
1223
|
|
|
|
|
|
|
C<'boolean'>.pm for handling boolean types. That allows you to dump the data |
|
1224
|
|
|
|
|
|
|
structure with one of the JSON modules without losing information about |
|
1225
|
|
|
|
|
|
|
booleans. |
|
1226
|
|
|
|
|
|
|
|
|
1227
|
|
|
|
|
|
|
=item Numbers |
|
1228
|
|
|
|
|
|
|
|
|
1229
|
|
|
|
|
|
|
Numbers are created as real numbers instead of strings, so that they are |
|
1230
|
|
|
|
|
|
|
dumped correctly by modules like L<JSON::PP> or L<JSON::XS>, for example. |
|
1231
|
|
|
|
|
|
|
|
|
1232
|
|
|
|
|
|
|
=item Complex Keys |
|
1233
|
|
|
|
|
|
|
|
|
1234
|
|
|
|
|
|
|
Mapping Keys in YAML can be more than just scalars. Of course, you can't load |
|
1235
|
|
|
|
|
|
|
that into a native perl structure. The Constructor will stringify those keys |
|
1236
|
|
|
|
|
|
|
with L<Data::Dumper> instead of just returning something like |
|
1237
|
|
|
|
|
|
|
C<HASH(0x55dc1b5d0178)>. |
|
1238
|
|
|
|
|
|
|
|
|
1239
|
|
|
|
|
|
|
Example: |
|
1240
|
|
|
|
|
|
|
|
|
1241
|
|
|
|
|
|
|
use YAML::PP; |
|
1242
|
|
|
|
|
|
|
use JSON::PP; |
|
1243
|
|
|
|
|
|
|
my $ypp = YAML::PP->new; |
|
1244
|
|
|
|
|
|
|
my $coder = JSON::PP->new->ascii->pretty->allow_nonref->canonical; |
|
1245
|
|
|
|
|
|
|
my $yaml = <<'EOM'; |
|
1246
|
|
|
|
|
|
|
complex: |
|
1247
|
|
|
|
|
|
|
? |
|
1248
|
|
|
|
|
|
|
? |
|
1249
|
|
|
|
|
|
|
a: 1 |
|
1250
|
|
|
|
|
|
|
c: 2 |
|
1251
|
|
|
|
|
|
|
: 23 |
|
1252
|
|
|
|
|
|
|
: 42 |
|
1253
|
|
|
|
|
|
|
EOM |
|
1254
|
|
|
|
|
|
|
my $data = $yppl->load_string($yaml); |
|
1255
|
|
|
|
|
|
|
say $coder->encode($data); |
|
1256
|
|
|
|
|
|
|
__END__ |
|
1257
|
|
|
|
|
|
|
{ |
|
1258
|
|
|
|
|
|
|
"complex" : { |
|
1259
|
|
|
|
|
|
|
"{'{a => 1,c => 2}' => 23}" : 42 |
|
1260
|
|
|
|
|
|
|
} |
|
1261
|
|
|
|
|
|
|
} |
|
1262
|
|
|
|
|
|
|
|
|
1263
|
|
|
|
|
|
|
=back |
|
1264
|
|
|
|
|
|
|
|
|
1265
|
|
|
|
|
|
|
TODO: |
|
1266
|
|
|
|
|
|
|
|
|
1267
|
|
|
|
|
|
|
=over 4 |
|
1268
|
|
|
|
|
|
|
|
|
1269
|
|
|
|
|
|
|
=item Parse Tree |
|
1270
|
|
|
|
|
|
|
|
|
1271
|
|
|
|
|
|
|
I would like to generate a complete parse tree, that allows you to manipulate |
|
1272
|
|
|
|
|
|
|
the data structure and also dump it, including all whitespaces and comments. |
|
1273
|
|
|
|
|
|
|
The spec says that this is throwaway content, but I read that many people |
|
1274
|
|
|
|
|
|
|
wish to be able to keep the comments. |
|
1275
|
|
|
|
|
|
|
|
|
1276
|
|
|
|
|
|
|
=back |
|
1277
|
|
|
|
|
|
|
|
|
1278
|
|
|
|
|
|
|
=head2 YAML::PP::Dumper, YAML::PP::Emitter |
|
1279
|
|
|
|
|
|
|
|
|
1280
|
|
|
|
|
|
|
The Dumper should be able to dump strings correctly, adding quotes |
|
1281
|
|
|
|
|
|
|
whenever a plain scalar would look like a special string, like C<true>, |
|
1282
|
|
|
|
|
|
|
or when it contains or starts with characters that are not allowed. |
|
1283
|
|
|
|
|
|
|
|
|
1284
|
|
|
|
|
|
|
Most strings will be dumped as plain scalars without quotes. If they |
|
1285
|
|
|
|
|
|
|
contain special characters or have a special meaning, they will be dumped |
|
1286
|
|
|
|
|
|
|
with single quotes. If they contain control characters, including <"\n">, |
|
1287
|
|
|
|
|
|
|
they will be dumped with double quotes. |
|
1288
|
|
|
|
|
|
|
|
|
1289
|
|
|
|
|
|
|
It will recognize JSON::PP::Boolean and boolean.pm objects and dump them |
|
1290
|
|
|
|
|
|
|
correctly. |
|
1291
|
|
|
|
|
|
|
|
|
1292
|
|
|
|
|
|
|
Numbers which also have a C<PV> flag will be recognized as numbers and not |
|
1293
|
|
|
|
|
|
|
as strings: |
|
1294
|
|
|
|
|
|
|
|
|
1295
|
|
|
|
|
|
|
my $int = 23; |
|
1296
|
|
|
|
|
|
|
say "int: $int"; # $int will now also have a PV flag |
|
1297
|
|
|
|
|
|
|
|
|
1298
|
|
|
|
|
|
|
That means that if you accidentally use a string in numeric context, it will |
|
1299
|
|
|
|
|
|
|
also be recognized as a number: |
|
1300
|
|
|
|
|
|
|
|
|
1301
|
|
|
|
|
|
|
my $string = "23"; |
|
1302
|
|
|
|
|
|
|
my $something = $string + 0; |
|
1303
|
|
|
|
|
|
|
print $yp->dump_string($string); |
|
1304
|
|
|
|
|
|
|
# will be emitted as an integer without quotes! |
|
1305
|
|
|
|
|
|
|
|
|
1306
|
|
|
|
|
|
|
The layout is like libyaml output: |
|
1307
|
|
|
|
|
|
|
|
|
1308
|
|
|
|
|
|
|
key: |
|
1309
|
|
|
|
|
|
|
- a |
|
1310
|
|
|
|
|
|
|
- b |
|
1311
|
|
|
|
|
|
|
- c |
|
1312
|
|
|
|
|
|
|
--- |
|
1313
|
|
|
|
|
|
|
- key1: 1 |
|
1314
|
|
|
|
|
|
|
key2: 2 |
|
1315
|
|
|
|
|
|
|
key3: 3 |
|
1316
|
|
|
|
|
|
|
--- |
|
1317
|
|
|
|
|
|
|
- - a1 |
|
1318
|
|
|
|
|
|
|
- a2 |
|
1319
|
|
|
|
|
|
|
- - b1 |
|
1320
|
|
|
|
|
|
|
- b2 |
|
1321
|
|
|
|
|
|
|
|
|
1322
|
|
|
|
|
|
|
=head1 FAQ - Frequently Asked Questions |
|
1323
|
|
|
|
|
|
|
|
|
1324
|
|
|
|
|
|
|
=over |
|
1325
|
|
|
|
|
|
|
|
|
1326
|
|
|
|
|
|
|
=item Are C<<<> merge keys supported? |
|
1327
|
|
|
|
|
|
|
|
|
1328
|
|
|
|
|
|
|
Yes, this can be enabled optionally, see L<YAML::PP::Schema::Merge> |
|
1329
|
|
|
|
|
|
|
|
|
1330
|
|
|
|
|
|
|
=item Is there a linter / formatter for YAML |
|
1331
|
|
|
|
|
|
|
|
|
1332
|
|
|
|
|
|
|
There is the widely used L<"yamllint"|https://yamllint.readthedocs.io/>, based on |
|
1333
|
|
|
|
|
|
|
python's PyYAML. It is very configurable and will report errors or warnings. |
|
1334
|
|
|
|
|
|
|
It cannot format. |
|
1335
|
|
|
|
|
|
|
|
|
1336
|
|
|
|
|
|
|
Now there is also L<YAML::Tidy>, which will format the given file according |
|
1337
|
|
|
|
|
|
|
to your configuration. So far only a few configuration options exist, but |
|
1338
|
|
|
|
|
|
|
they can already be quite helpful. |
|
1339
|
|
|
|
|
|
|
|
|
1340
|
|
|
|
|
|
|
=back |
|
1341
|
|
|
|
|
|
|
|
|
1342
|
|
|
|
|
|
|
=head1 Which YAML module should I use? |
|
1343
|
|
|
|
|
|
|
|
|
1344
|
|
|
|
|
|
|
There are many YAML modules on CPAN. For historical reasons some of them |
|
1345
|
|
|
|
|
|
|
aren't handling YAML correctly. |
|
1346
|
|
|
|
|
|
|
|
|
1347
|
|
|
|
|
|
|
Most of them are not compatible with the YAML spec and with each other, meaning |
|
1348
|
|
|
|
|
|
|
they can interpret the same YAML differently. |
|
1349
|
|
|
|
|
|
|
|
|
1350
|
|
|
|
|
|
|
The behaviours we are discussing here can be divided into parsing issues |
|
1351
|
|
|
|
|
|
|
(syntax) and loading/constructing issues (for example type resolving which |
|
1352
|
|
|
|
|
|
|
decides what is a number, boolean or null). |
|
1353
|
|
|
|
|
|
|
|
|
1354
|
|
|
|
|
|
|
See also L<https://matrix.yaml.info/> (parsing) and |
|
1355
|
|
|
|
|
|
|
L<https://perlpunk.github.io/YAML-PP-p5/schema-examples.html> (loading). |
|
1356
|
|
|
|
|
|
|
|
|
1357
|
|
|
|
|
|
|
=over |
|
1358
|
|
|
|
|
|
|
|
|
1359
|
|
|
|
|
|
|
=item L<"YAML.pm"|YAML> |
|
1360
|
|
|
|
|
|
|
|
|
1361
|
|
|
|
|
|
|
It was written even before the YAML 1.0 spec was finished and by that enabled |
|
1362
|
|
|
|
|
|
|
perl users to process YAML very early. It might work for you if you have simple |
|
1363
|
|
|
|
|
|
|
data, but it's missing quite some features and can also produce YAML that |
|
1364
|
|
|
|
|
|
|
doesn't roundtrip. Nowadays it might be a good idea to switch. |
|
1365
|
|
|
|
|
|
|
|
|
1366
|
|
|
|
|
|
|
=item L<YAML::XS> |
|
1367
|
|
|
|
|
|
|
|
|
1368
|
|
|
|
|
|
|
A libyaml binding that is robust and widely used. However, there are two |
|
1369
|
|
|
|
|
|
|
things to consider. |
|
1370
|
|
|
|
|
|
|
|
|
1371
|
|
|
|
|
|
|
1. (syntax) libyaml diverged from the spec in several aspects. They are rare though. |
|
1372
|
|
|
|
|
|
|
2. The type resolving does not adhere to YAML 1.1 or YAML 1.2, meaning it is |
|
1373
|
|
|
|
|
|
|
incompatible with other YAML libraries in perl or other languages. |
|
1374
|
|
|
|
|
|
|
|
|
1375
|
|
|
|
|
|
|
=item L<YAML::Tiny> |
|
1376
|
|
|
|
|
|
|
|
|
1377
|
|
|
|
|
|
|
It implements both a tiny subset of YAML, but also a superset. Meaning |
|
1378
|
|
|
|
|
|
|
it will happily accept some YAML documents that are not officially valid. |
|
1379
|
|
|
|
|
|
|
Type resolving is also not implemented according to the spec. |
|
1380
|
|
|
|
|
|
|
|
|
1381
|
|
|
|
|
|
|
=item L<YAML::Syck> |
|
1382
|
|
|
|
|
|
|
|
|
1383
|
|
|
|
|
|
|
A binding to libsyck. It is even less compatible to YAML than libyaml. Also |
|
1384
|
|
|
|
|
|
|
type resolving is not implemented according to the spec. |
|
1385
|
|
|
|
|
|
|
|
|
1386
|
|
|
|
|
|
|
=item L<YAML::PP> |
|
1387
|
|
|
|
|
|
|
|
|
1388
|
|
|
|
|
|
|
Regarding YAML syntax, it is the second most YAML 1.2 compatible perl module. |
|
1389
|
|
|
|
|
|
|
The cases it cannot (yet) parse are not relevant in perl programming, |
|
1390
|
|
|
|
|
|
|
e.g. hash keys that are not strings. |
|
1391
|
|
|
|
|
|
|
Regarding type resolving, it is compatible with the YAML 1.2 Core schema, |
|
1392
|
|
|
|
|
|
|
so it should be possible to exchange data as YAML with other libraries |
|
1393
|
|
|
|
|
|
|
in other languages. |
|
1394
|
|
|
|
|
|
|
One downside is that it is the slowest perl YAML module. |
|
1395
|
|
|
|
|
|
|
|
|
1396
|
|
|
|
|
|
|
=item L<YAML::Parser> |
|
1397
|
|
|
|
|
|
|
|
|
1398
|
|
|
|
|
|
|
This is a parser generated by the YAML grammar, and it's passing all |
|
1399
|
|
|
|
|
|
|
official tests. A L<YAML::PP::Ref> frontend exists that you can use just like |
|
1400
|
|
|
|
|
|
|
YAML::PP. |
|
1401
|
|
|
|
|
|
|
It is quite slow (although it might be ok for small files depending on the |
|
1402
|
|
|
|
|
|
|
use case). |
|
1403
|
|
|
|
|
|
|
The error messages it creates on invalid YAML are not helpful currently. |
|
1404
|
|
|
|
|
|
|
|
|
1405
|
|
|
|
|
|
|
=item L<YAML::PP::LibYAML> |
|
1406
|
|
|
|
|
|
|
|
|
1407
|
|
|
|
|
|
|
This combines the L<YAML::LibYAML::API> binding for parsing with the YAML::PP |
|
1408
|
|
|
|
|
|
|
frontend for loading and type resolving. |
|
1409
|
|
|
|
|
|
|
It is faster than YAML::PP but slower than YAML::XS. |
|
1410
|
|
|
|
|
|
|
The divergence from the YAML spec regarding syntax is usually not a problem, |
|
1411
|
|
|
|
|
|
|
and at the same time you have the advantage of being compatible to the |
|
1412
|
|
|
|
|
|
|
YAML 1.2 Core Schema. |
|
1413
|
|
|
|
|
|
|
|
|
1414
|
|
|
|
|
|
|
=back |
|
1415
|
|
|
|
|
|
|
|
|
1416
|
|
|
|
|
|
|
=head1 WHY |
|
1417
|
|
|
|
|
|
|
|
|
1418
|
|
|
|
|
|
|
Why did I start to write a new YAML module? |
|
1419
|
|
|
|
|
|
|
|
|
1420
|
|
|
|
|
|
|
All the available parsers and loaders for Perl are behaving differently, |
|
1421
|
|
|
|
|
|
|
and more important, aren't conforming to the spec. L<YAML::XS> is |
|
1422
|
|
|
|
|
|
|
doing pretty well, but C<libyaml> only handles YAML 1.1 and diverges |
|
1423
|
|
|
|
|
|
|
a bit from the spec. The pure perl loaders lack support for a number of |
|
1424
|
|
|
|
|
|
|
features. |
|
1425
|
|
|
|
|
|
|
|
|
1426
|
|
|
|
|
|
|
I was going over L<YAML>.pm issues end of 2016, integrating old patches |
|
1427
|
|
|
|
|
|
|
from rt.cpan.org and creating some pull requests myself. I realized |
|
1428
|
|
|
|
|
|
|
that it would be difficult to patch YAML.pm to parse YAML 1.1 or even 1.2, |
|
1429
|
|
|
|
|
|
|
and it would also break existing usages relying on the current behaviour. |
|
1430
|
|
|
|
|
|
|
|
|
1431
|
|
|
|
|
|
|
|
|
1432
|
|
|
|
|
|
|
In 2016 Ingy döt Net initiated two really cool projects: |
|
1433
|
|
|
|
|
|
|
|
|
1434
|
|
|
|
|
|
|
=over 4 |
|
1435
|
|
|
|
|
|
|
|
|
1436
|
|
|
|
|
|
|
=item L<"YAML TEST SUITE"> |
|
1437
|
|
|
|
|
|
|
|
|
1438
|
|
|
|
|
|
|
=item L<"YAML EDITOR"> |
|
1439
|
|
|
|
|
|
|
|
|
1440
|
|
|
|
|
|
|
=back |
|
1441
|
|
|
|
|
|
|
|
|
1442
|
|
|
|
|
|
|
These projects are a big help for any developer. So I got the idea |
|
1443
|
|
|
|
|
|
|
to write my own parser and started on New Year's Day 2017. |
|
1444
|
|
|
|
|
|
|
Without the test suite and the editor I would have never started this. |
|
1445
|
|
|
|
|
|
|
|
|
1446
|
|
|
|
|
|
|
I also started another YAML Test project which allows one to get a quick |
|
1447
|
|
|
|
|
|
|
overview of which frameworks support which YAML features: |
|
1448
|
|
|
|
|
|
|
|
|
1449
|
|
|
|
|
|
|
=over 4 |
|
1450
|
|
|
|
|
|
|
|
|
1451
|
|
|
|
|
|
|
=item L<"YAML TEST MATRIX"> |
|
1452
|
|
|
|
|
|
|
|
|
1453
|
|
|
|
|
|
|
=back |
|
1454
|
|
|
|
|
|
|
|
|
1455
|
|
|
|
|
|
|
=head2 YAML TEST SUITE |
|
1456
|
|
|
|
|
|
|
|
|
1457
|
|
|
|
|
|
|
L<https://github.com/yaml/yaml-test-suite> |
|
1458
|
|
|
|
|
|
|
|
|
1459
|
|
|
|
|
|
|
It contains almost 400 test cases and expected parsing events and more. |
|
1460
|
|
|
|
|
|
|
There will be more tests coming. This test suite allows you to write parsers |
|
1461
|
|
|
|
|
|
|
without turning the examples from the Specification into tests yourself. |
|
1462
|
|
|
|
|
|
|
Also the examples aren't completely covering all cases - the test suite |
|
1463
|
|
|
|
|
|
|
aims to do that. |
|
1464
|
|
|
|
|
|
|
|
|
1465
|
|
|
|
|
|
|
Thanks also to Felix Krause, who is writing a YAML parser in Nim. |
|
1466
|
|
|
|
|
|
|
He turned all the spec examples into test cases. |
|
1467
|
|
|
|
|
|
|
|
|
1468
|
|
|
|
|
|
|
=head2 YAML EDITOR |
|
1469
|
|
|
|
|
|
|
|
|
1470
|
|
|
|
|
|
|
This is a tool to play around with several YAML parsers and loaders in vim. |
|
1471
|
|
|
|
|
|
|
|
|
1472
|
|
|
|
|
|
|
L<https://github.com/yaml/yaml-editor> |
|
1473
|
|
|
|
|
|
|
|
|
1474
|
|
|
|
|
|
|
The project contains the code to build the frameworks (16 as of this |
|
1475
|
|
|
|
|
|
|
writing) and put it into one big Docker image. |
|
1476
|
|
|
|
|
|
|
|
|
1477
|
|
|
|
|
|
|
It also contains the yaml-editor itself, which will start a vim in the docker |
|
1478
|
|
|
|
|
|
|
container. It uses a lot of funky vimscript that makes playing with it easy |
|
1479
|
|
|
|
|
|
|
and useful. You can choose which frameworks you want to test and see the |
|
1480
|
|
|
|
|
|
|
output in a grid of vim windows. |
|
1481
|
|
|
|
|
|
|
|
|
1482
|
|
|
|
|
|
|
Especially when writing a parser it is extremely helpful to have all |
|
1483
|
|
|
|
|
|
|
the test cases and be able to play around with your own examples to see |
|
1484
|
|
|
|
|
|
|
how they are handled. |
|
1485
|
|
|
|
|
|
|
|
|
1486
|
|
|
|
|
|
|
=head2 YAML TEST MATRIX |
|
1487
|
|
|
|
|
|
|
|
|
1488
|
|
|
|
|
|
|
I was curious to see how the different frameworks handle the test cases, |
|
1489
|
|
|
|
|
|
|
so, using the test suite and the docker image, I wrote some code that runs |
|
1490
|
|
|
|
|
|
|
the tests, manipulates the output to compare it with the expected output, |
|
1491
|
|
|
|
|
|
|
and created a matrix view. |
|
1492
|
|
|
|
|
|
|
|
|
1493
|
|
|
|
|
|
|
L<https://github.com/perlpunk/yaml-test-matrix> |
|
1494
|
|
|
|
|
|
|
|
|
1495
|
|
|
|
|
|
|
You can find the latest build at L<https://matrix.yaml.info> |
|
1496
|
|
|
|
|
|
|
|
|
1497
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
|
1498
|
|
|
|
|
|
|
|
|
1499
|
|
|
|
|
|
|
=over |
|
1500
|
|
|
|
|
|
|
|
|
1501
|
|
|
|
|
|
|
=item Ingy döt Net |
|
1502
|
|
|
|
|
|
|
|
|
1503
|
|
|
|
|
|
|
Ingy is one of the creators of YAML. In 2016 he started the YAML Test Suite |
|
1504
|
|
|
|
|
|
|
and the YAML Editor. He also made useful suggestions on the class |
|
1505
|
|
|
|
|
|
|
hierarchy of YAML::PP. |
|
1506
|
|
|
|
|
|
|
|
|
1507
|
|
|
|
|
|
|
=item Felix "flyx" Krause |
|
1508
|
|
|
|
|
|
|
|
|
1509
|
|
|
|
|
|
|
Felix answered countless questions about the YAML Specification. |
|
1510
|
|
|
|
|
|
|
|
|
1511
|
|
|
|
|
|
|
=back |
|
1512
|
|
|
|
|
|
|
|
|
1513
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
1514
|
|
|
|
|
|
|
|
|
1515
|
|
|
|
|
|
|
=over |
|
1516
|
|
|
|
|
|
|
|
|
1517
|
|
|
|
|
|
|
=item L<YAML> |
|
1518
|
|
|
|
|
|
|
|
|
1519
|
|
|
|
|
|
|
=item L<YAML::XS> |
|
1520
|
|
|
|
|
|
|
|
|
1521
|
|
|
|
|
|
|
=item L<YAML::Syck> |
|
1522
|
|
|
|
|
|
|
|
|
1523
|
|
|
|
|
|
|
=item L<YAML::Tiny> |
|
1524
|
|
|
|
|
|
|
|
|
1525
|
|
|
|
|
|
|
=item L<YAML::PP::LibYAML> |
|
1526
|
|
|
|
|
|
|
|
|
1527
|
|
|
|
|
|
|
=item L<YAML::LibYAML::API> |
|
1528
|
|
|
|
|
|
|
|
|
1529
|
|
|
|
|
|
|
=item L<YAML::Tidy> |
|
1530
|
|
|
|
|
|
|
|
|
1531
|
|
|
|
|
|
|
=item L<https://www.yaml.info/> |
|
1532
|
|
|
|
|
|
|
|
|
1533
|
|
|
|
|
|
|
=back |
|
1534
|
|
|
|
|
|
|
|
|
1535
|
|
|
|
|
|
|
=head1 SPONSORS |
|
1536
|
|
|
|
|
|
|
|
|
1537
|
|
|
|
|
|
|
The Perl Foundation L<https://www.perlfoundation.org/> sponsored this project |
|
1538
|
|
|
|
|
|
|
(and the YAML Test Suite) with a grant of 2500 USD in 2017-2018. |
|
1539
|
|
|
|
|
|
|
|
|
1540
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
1541
|
|
|
|
|
|
|
|
|
1542
|
|
|
|
|
|
|
Copyright 2017-2022 by Tina Müller |
|
1543
|
|
|
|
|
|
|
|
|
1544
|
|
|
|
|
|
|
This library is free software and may be distributed under the same terms |
|
1545
|
|
|
|
|
|
|
as perl itself. |
|
1546
|
|
|
|
|
|
|
|
|
1547
|
|
|
|
|
|
|
=cut |