-
Notifications
You must be signed in to change notification settings - Fork 8
/
PP.pm
1515 lines (1067 loc) · 36.5 KB
/
PP.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ABSTRACT: YAML 1.2 Processor
use strict;
use warnings;
package YAML::PP;
our $VERSION = '0.000'; # VERSION
use YAML::PP::Schema;
use YAML::PP::Schema::JSON;
use YAML::PP::Loader;
use YAML::PP::Dumper;
use Scalar::Util qw/ blessed /;
use Carp qw/ croak /;
use base 'Exporter';
our @EXPORT_OK = qw/ Load LoadFile Dump DumpFile /;
my %YAML_VERSIONS = ('1.1' => 1, '1.2' => 1);
sub new {
my ($class, %args) = @_;
my $bool = delete $args{boolean};
$bool = 'perl' unless defined $bool;
my $schemas = delete $args{schema} || ['+'];
my $cyclic_refs = delete $args{cyclic_refs} || 'fatal';
my $indent = delete $args{indent};
my $width = delete $args{width};
my $writer = delete $args{writer};
my $header = delete $args{header};
my $footer = delete $args{footer};
my $duplicate_keys = delete $args{duplicate_keys};
my $yaml_version = $class->_arg_yaml_version(delete $args{yaml_version});
my $default_yaml_version = $yaml_version->[0];
my $version_directive = delete $args{version_directive};
my $preserve = delete $args{preserve};
my $parser = delete $args{parser};
my $emitter = delete $args{emitter} || {
indent => $indent,
width => $width,
writer => $writer,
};
if (keys %args) {
die "Unexpected arguments: " . join ', ', sort keys %args;
}
my %schemas;
for my $v (@$yaml_version) {
my $schema;
if (blessed($schemas) and $schemas->isa('YAML::PP::Schema')) {
$schema = $schemas;
}
else {
$schema = YAML::PP::Schema->new(
boolean => $bool,
yaml_version => $v,
);
$schema->load_subschemas(@$schemas);
}
$schemas{ $v } = $schema;
}
my $default_schema = $schemas{ $default_yaml_version };
my $loader = YAML::PP::Loader->new(
schemas => \%schemas,
cyclic_refs => $cyclic_refs,
parser => $parser,
default_yaml_version => $default_yaml_version,
preserve => $preserve,
duplicate_keys => $duplicate_keys,
);
my $dumper = YAML::PP::Dumper->new(
schema => $default_schema,
emitter => $emitter,
header => $header,
footer => $footer,
version_directive => $version_directive,
preserve => $preserve,
);
my $self = bless {
schema => \%schemas,
loader => $loader,
dumper => $dumper,
}, $class;
return $self;
}
sub clone {
my ($self) = @_;
my $clone = {
schema => $self->schema,
loader => $self->loader->clone,
dumper => $self->dumper->clone,
};
return bless $clone, ref $self;
}
sub _arg_yaml_version {
my ($class, $version) = @_;
my @versions = ('1.2');
if (defined $version) {
@versions = ();
if (not ref $version) {
$version = [$version];
}
for my $v (@$version) {
unless ($YAML_VERSIONS{ $v }) {
croak "YAML Version '$v' not supported";
}
push @versions, $v;
}
}
return \@versions;
}
sub loader {
if (@_ > 1) {
$_[0]->{loader} = $_[1]
}
return $_[0]->{loader};
}
sub dumper {
if (@_ > 1) {
$_[0]->{dumper} = $_[1]
}
return $_[0]->{dumper};
}
sub schema {
if (@_ > 1) { $_[0]->{schema}->{'1.2'} = $_[1] }
return $_[0]->{schema}->{'1.2'};
}
sub default_schema {
my ($self, %args) = @_;
my $schema = YAML::PP::Schema->new(
boolean => $args{boolean},
);
$schema->load_subschemas(qw/ Core /);
return $schema;
}
sub load_string {
my ($self, $yaml) = @_;
return $self->loader->load_string($yaml);
}
sub load_file {
my ($self, $file) = @_;
return $self->loader->load_file($file);
}
sub dump {
my ($self, @data) = @_;
return $self->dumper->dump(@data);
}
sub dump_string {
my ($self, @data) = @_;
return $self->dumper->dump_string(@data);
}
sub dump_file {
my ($self, $file, @data) = @_;
return $self->dumper->dump_file($file, @data);
}
# legagy interface
sub Load {
my ($yaml) = @_;
YAML::PP->new->load_string($yaml);
}
sub LoadFile {
my ($file) = @_;
YAML::PP->new->load_file($file);
}
sub Dump {
my (@data) = @_;
YAML::PP->new->dump_string(@data);
}
sub DumpFile {
my ($file, @data) = @_;
YAML::PP->new->dump_file($file, @data);
}
sub preserved_scalar {
my ($self, $value, %args) = @_;
my $scalar = YAML::PP::Preserve::Scalar->new(
value => $value,
%args,
);
return $scalar;
}
sub preserved_mapping {
my ($self, $hash, %args) = @_;
my $data = {};
tie %$data, 'YAML::PP::Preserve::Hash';
%$data = %$hash;
my $t = tied %$data;
$t->{style} = $args{style};
$t->{alias} = $args{alias};
return $data;
}
sub preserved_sequence {
my ($self, $array, %args) = @_;
my $data = [];
tie @$data, 'YAML::PP::Preserve::Array';
push @$data, @$array;
my $t = tied @$data;
$t->{style} = $args{style};
$t->{alias} = $args{alias};
return $data;
}
package YAML::PP::Preserve::Hash;
# experimental
use Tie::Hash;
use base qw/ Tie::StdHash /;
use Scalar::Util qw/ reftype blessed /;
sub TIEHASH {
my ($class, %args) = @_;
my $self = bless {
keys => [keys %args],
data => { %args },
}, $class;
}
sub STORE {
my ($self, $key, $val) = @_;
my $keys = $self->{keys};
unless (exists $self->{data}->{ $key }) {
push @$keys, $key;
}
if (ref $val and not blessed($val)) {
if (reftype($val) eq 'HASH' and not tied %$val) {
tie %$val, 'YAML::PP::Preserve::Hash', %$val;
}
elsif (reftype($val) eq 'ARRAY' and not tied @$val) {
tie @$val, 'YAML::PP::Preserve::Array', @$val;
}
}
$self->{data}->{ $key } = $val;
}
sub FIRSTKEY {
my ($self) = @_;
return $self->{keys}->[0];
}
sub NEXTKEY {
my ($self, $last) = @_;
my $keys = $self->{keys};
for my $i (0 .. $#$keys) {
if ("$keys->[ $i ]" eq "$last") {
return $keys->[ $i + 1 ];
}
}
return;
}
sub FETCH {
my ($self, $key) = @_;
my $val = $self->{data}->{ $key };
}
sub DELETE {
my ($self, $key) = @_;
@{ $self->{keys} } = grep { "$_" ne "$key" } @{ $self->{keys} };
delete $self->{data}->{ $key };
}
sub EXISTS {
my ($self, $key) = @_;
return exists $self->{data}->{ $key };
}
sub CLEAR {
my ($self) = @_;
$self->{keys} = [];
$self->{data} = {};
}
sub SCALAR {
my ($self) = @_;
return scalar %{ $self->{data} };
}
package YAML::PP::Preserve::Array;
# experimental
use Tie::Array;
use base qw/ Tie::StdArray /;
use Scalar::Util qw/ reftype blessed /;
sub TIEARRAY {
my ($class, @items) = @_;
my $self = bless {
data => [@items],
}, $class;
return $self;
}
sub FETCH {
my ($self, $i) = @_;
return $self->{data}->[ $i ];
}
sub FETCHSIZE {
my ($self) = @_;
return $#{ $self->{data} } + 1;
}
sub _preserve {
my ($val) = @_;
if (ref $val and not blessed($val)) {
if (reftype($val) eq 'HASH' and not tied %$val) {
tie %$val, 'YAML::PP::Preserve::Hash', %$val;
}
elsif (reftype($val) eq 'ARRAY' and not tied @$val) {
tie @$val, 'YAML::PP::Preserve::Array', @$val;
}
}
return $val;
}
sub STORE {
my ($self, $i, $val) = @_;
_preserve($val);
$self->{data}->[ $i ] = $val;
}
sub PUSH {
my ($self, @args) = @_;
push @{ $self->{data} }, map { _preserve $_ } @args;
}
sub STORESIZE {
my ($self, $i) = @_;
$#{ $self->{data} } = $i - 1;
}
sub DELETE {
my ($self, $i) = @_;
delete $self->{data}->[ $i ];
}
sub EXISTS {
my ($self, $i) = @_;
return exists $self->{data}->[ $i ];
}
sub CLEAR {
my ($self) = @_;
$self->{data} = [];
}
sub SHIFT {
my ($self) = @_;
shift @{ $self->{data} };
}
sub UNSHIFT {
my ($self, @args) = @_;
unshift @{ $self->{data} }, map { _preserve $_ } @args;
}
sub SPLICE {
my ($self, $offset, $length, @args) = @_;
splice @{ $self->{data} }, $offset, $length, map { _preserve $_ } @args;
}
sub EXTEND {}
package YAML::PP::Preserve::Scalar;
use overload
fallback => 1,
'+' => \&value,
'""' => \&value,
'bool' => \&value,
;
sub new {
my ($class, %args) = @_;
my $self = {
%args,
};
bless $self, $class;
}
sub value { $_[0]->{value} }
sub tag { $_[0]->{tag} }
sub style { $_[0]->{style} || 0 }
sub alias { $_[0]->{alias} }
1;
__END__
=pod
=encoding utf-8
=head1 NAME
YAML::PP - YAML 1.2 processor
=head1 SYNOPSIS
WARNING: Most of the inner API is not stable yet.
Here are a few examples of the basic load and dump methods:
use YAML::PP;
my $ypp = YAML::PP->new;
my $yaml = <<'EOM';
--- # Document one is a mapping
name: Tina
age: 29
favourite language: Perl
--- # Document two is a sequence
- plain string
- 'in single quotes'
- "in double quotes we have escapes! like \t and \n"
- | # a literal block scalar
line1
line2
- > # a folded block scalar
this is all one
single line because the
linebreaks will be folded
EOM
my @documents = $ypp->load_string($yaml);
my @documents = $ypp->load_file($filename);
my $yaml = $ypp->dump_string($data1, $data2);
$ypp->dump_file($filename, $data1, $data2);
# Enable perl data types and objects
my $ypp = YAML::PP->new(schema => [qw/ + Perl /]);
my $yaml = $yp->dump_string($data_with_perl_objects);
# Legacy interface
use YAML::PP qw/ Load Dump LoadFile DumpFile /;
my @documents = Load($yaml);
my @documents = LoadFile($filename);
my @documents = LoadFile($filehandle);
my $yaml = = Dump(@documents);
DumpFile($filename, @documents);
DumpFile($filenhandle @documents);
Some utility scripts, mostly useful for debugging:
# Load YAML into a data structure and dump with Data::Dumper
yamlpp-load < file.yaml
# Load and Dump
yamlpp-load-dump < file.yaml
# Print the events from the parser in yaml-test-suite format
yamlpp-events < file.yaml
# Parse and emit events directly without loading
yamlpp-parse-emit < file.yaml
# Create ANSI colored YAML. Can also be useful for invalid YAML, showing
# you the exact location of the error
yamlpp-highlight < file.yaml
=head1 DESCRIPTION
YAML::PP is a modular YAML processor.
It aims to support C<YAML 1.2> and C<YAML 1.1>. See L<https://yaml.org/>.
Some (rare) syntax elements are not yet supported and documented below.
YAML is a serialization language. The YAML input is called "YAML Stream".
A stream consists of one or more "Documents", separated by a line with a
document start marker C<--->. A document optionally ends with the document
end marker C<...>.
This allows one to process continuous streams additionally to a fixed input
file or string.
The YAML::PP frontend will currently load all documents, and return only
the first if called with scalar context.
The YAML backend is implemented in a modular way that allows one to add
custom handling of YAML tags, perl objects and data types. The inner API
is not yet stable. Suggestions welcome.
You can check out all current parse and load results from the
yaml-test-suite here:
L<https://perlpunk.github.io/YAML-PP-p5/test-suite.html>
=head1 METHODS
=head2 new
my $ypp = YAML::PP->new;
# use YAML 1.2 Failsafe Schema
my $ypp = YAML::PP->new( schema => ['Failsafe'] );
# use YAML 1.2 JSON Schema
my $ypp = YAML::PP->new( schema => ['JSON'] );
# use YAML 1.2 Core Schema
my $ypp = YAML::PP->new( schema => ['Core'] );
# Die when detecting cyclic references
my $ypp = YAML::PP->new( cyclic_refs => 'fatal' );
my $ypp = YAML::PP->new(
boolean => 'perl',
schema => ['Core'],
cyclic_refs => 'fatal',
indent => 4,
header => 1,
footer => 0,
version_directive => 0,
);
Options:
=over
=item boolean
Values: C<perl> (currently default), C<JSON::PP>, C<boolean>, C<perl_experimental>
This option is for loading and dumping.
In case of perl 5.36 and later, builtin booleans should work out of the box
(since YAML::PP >= 0.38.0).
print YAML::PP->new->dump_string([ builtin::true, !1 ]);
# ---
# - true
# - false
For earlier perl versions, you can use "pseudo" booleans like documented
in the following examples.
Examples:
# load/dump booleans via boolean.pm
my $ypp = YAML::PP->new( boolean => 'boolean' );
# load/dump booleans via JSON::PP::true/false
my $ypp = YAML::PP->new( boolean => 'JSON::PP' );
You can also specify more than one class, comma separated.
This is important for dumping.
boolean => 'JSON::PP,boolean'
Booleans will be loaded as JSON::PP::Booleans, but when dumping, also
'boolean' objects will be recognized
boolean => 'JSON::PP,*'
Booleans will be loaded as JSON::PP::Booleans, but when dumping, all
currently supported boolean classes will be recognized
boolean => '*'
Booleans will be loaded as perl booleans, but when dumping, all
currently supported boolean classes will be recognized
boolean => ''
Booleans will be loaded as perl booleans, but when dumping, nothing
will be recognized as booleans.
This option is for backwards compatibility for perl versions < 5.36,
if you rely on [!!1, !1] being dumped as [1, ''].
The option c<perl_experimental> was introduced when experimental boolean
support was added to perl 5.36. Since it will not be experimental anymore
in perl 5.40 \o/ the option is deprecated and the same as C<perl>.
=item schema
Default: C<['Core']>
This option is for loading and dumping.
Array reference. Here you can define what schema to use.
Supported standard Schemas are: C<Failsafe>, C<JSON>, C<Core>, C<YAML1_1>.
To get an overview how the different Schemas behave, see
L<https://perlpunk.github.io/YAML-PP-p5/schemas.html>
Additionally you can add further schemas, for example C<Merge>.
=item cyclic_refs
Default: C<fatal> (since 0.037)
Before the default was C<allow>, but this can lead to memory leaks
when loading on untrusted data, so it was changed to C<fatal> by default.
This option is for loading only.
Defines what to do when a cyclic reference is detected when loading.
# fatal - die
# warn - Just warn about them and replace with undef
# ignore - replace with undef
# allow - Default
=item duplicate_keys
Default: 0
Since version 0.027
This option is for loading.
The YAML Spec says duplicate mapping keys should be forbidden.
When set to true, duplicate keys in mappings are allowed (and will overwrite
the previous key).
When set to false, duplicate keys will result in an error when loading.
This is especially useful when you have a longer mapping and don't see
the duplicate key in your editor:
---
a: 1
b: 2
# .............
a: 23 # error
=item indent
Default: 2
This option is for dumping.
Use that many spaces for indenting
=item width
Since version 0.025
Default: 80
This option is for dumping.
Maximum columns when dumping.
This is only respected when dumping flow collections right now.
in the future it will be used also for wrapping long strings.
=item header
Default: 1
This option is for dumping.
Print document header C<--->
=item footer
Default: 0
This option is for dumping.
Print document footer C<...>
=item yaml_version
Since version 0.020
This option is for loading and dumping.
Default: C<1.2>
Note that in this case, a directive C<%YAML 1.1> will basically be ignored
and everything loaded with the C<1.2 Core> Schema.
If you want to support both YAML 1.1 and 1.2, you have to specify that, and the
schema (C<Core> or C<YAML1_1>) will be chosen automatically.
my $yp = YAML::PP->new(
yaml_version => ['1.2', '1.1'],
);
This is the same as
my $yp = YAML::PP->new(
schema => ['+'],
yaml_version => ['1.2', '1.1'],
);
because the C<+> stands for the default schema per version.
When loading, and there is no C<%YAML> directive, C<1.2> will be considered
as default, and the C<Core> schema will be used.
If there is a C<%YAML 1.1> directive, the C<YAML1_1> schema will be used.
Of course, you can also make C<1.1> the default:
my $yp = YAML::PP->new(
yaml_version => ['1.1', '1.2'],
);
You can also specify C<1.1> only:
my $yp = YAML::PP->new(
yaml_version => ['1.1'],
);
In this case also documents with C<%YAML 1.2> will be loaded with the C<YAML1_1>
schema.
=item version_directive
Since version 0.020
This option is for dumping.
Default: 0
Print Version Directive C<%YAML 1.2> (or C<%YAML 1.1>) on top of each YAML
document. It will use the first version specified in the C<yaml_version> option.
=item preserve
Since version 0.021
Default: false
This option is for loading and dumping.
Preserving scalar styles is still experimental.
use YAML::PP::Common qw/ :PRESERVE /;
# Preserve the order of hash keys
my $yp = YAML::PP->new( preserve => PRESERVE_ORDER );
# Preserve the quoting style of scalars
my $yp = YAML::PP->new( preserve => PRESERVE_SCALAR_STYLE );
# Preserve block/flow style (since 0.024)
my $yp = YAML::PP->new( preserve => PRESERVE_FLOW_STYLE );
# Preserve alias names (since 0.027)
my $yp = YAML::PP->new( preserve => PRESERVE_ALIAS );
# Combine, e.g. preserve order and scalar style
my $yp = YAML::PP->new( preserve => PRESERVE_ORDER | PRESERVE_SCALAR_STYLE );
Do NOT rely on the internal implementation of it.
If you load the following input:
---
z: 1
a: 2
---
- plain
- 'single'
- "double"
- |
literal
- >
folded
---
block mapping: &alias
flow sequence: [a, b]
same mapping: *alias
flow mapping: {a: b}
with this code:
my $yp = YAML::PP->new(
preserve => PRESERVE_ORDER | PRESERVE_SCALAR_STYLE
| PRESERVE_FLOW_STYLE | PRESERVE_ALIAS
);
my ($hash, $styles, $flow) = $yp->load_file($file);
$yp->dump_file($hash, $styles, $flow);
Then dumping it will return the same output.
Note that YAML allows repeated definition of anchors. They cannot be preserved
with YAML::PP right now. Example:
---
- &seq [a]
- *seq
- &seq [b]
- *seq
Because the data could be shuffled before dumping again, the anchor definition
could be broken. In this case repeated anchor names will be discarded when
loading and dumped with numeric anchors like usual.
Implementation:
When loading, hashes will be tied to an internal class
(C<YAML::PP::Preserve::Hash>) that keeps the key order.
Scalars will be returned as objects of an internal class
(C<YAML::PP::Preserve::Scalar>) with overloading. If you assign to such
a scalar, the object will be replaced by a simple scalar.
# assignment, style gets lost
$styles->[1] .= ' append';
You can also pass C<1> as a value. In this case all preserving options will be
enabled, also if there are new options added in the future.
There are also methods to create preserved nodes from scratch. See the
C<preserved_(scalar|mapping|sequence)> L<"METHODS"> below.
=back
=head2 load_string
my $doc = $ypp->load_string("foo: bar");
my @docs = $ypp->load_string("foo: bar\n---\n- a");
Input should be Unicode characters.
So if you read from a file, you should decode it, for example with
C<Encode::decode()>.
Note that in scalar context, C<load_string> and C<load_file> return the first
document (like L<YAML::Syck>), while L<YAML> and L<YAML::XS> return the
last.
=head2 load_file
my $doc = $ypp->load_file("file.yaml");
my @docs = $ypp->load_file("file.yaml");
Strings will be loaded as unicode characters.
=head2 dump_string
my $yaml = $ypp->dump_string($doc);
my $yaml = $ypp->dump_string($doc1, $doc2);
my $yaml = $ypp->dump_string(@docs);
Input strings should be Unicode characters.
Output will return Unicode characters.
So if you want to write that to a file (or pass to YAML::XS, for example),
you typically encode it via C<Encode::encode()>.
=head2 dump_file
$ypp->dump_file("file.yaml", $doc);
$ypp->dump_file("file.yaml", $doc1, $doc2);
$ypp->dump_file("file.yaml", @docs);
Input data should be Unicode characters.
=head2 dump
This will dump to a predefined writer. By default it will just use the
L<YAML::PP::Writer> and output a string.
my $writer = MyWriter->new(\my $output);
my $yp = YAML::PP->new(
writer => $writer,
);
$yp->dump($data);
=head2 preserved_scalar
Since version 0.024
Experimental. Please report bugs or let me know this is useful and works.
You can define a certain scalar style when dumping data.
Figuring out the best style is a hard task and practically impossible to get
it right for all cases. It's also a matter of taste.
use YAML::PP::Common qw/ PRESERVE_SCALAR_STYLE YAML_LITERAL_SCALAR_STYLE /;
my $yp = YAML::PP->new(
preserve => PRESERVE_SCALAR_STYLE,
);
# a single linebreak would normally be dumped with double quotes: "\n"
my $scalar = $yp->preserved_scalar("\n", style => YAML_LITERAL_SCALAR_STYLE );
my $data = { literal => $scalar };
my $dump = $yp->dump_string($data);
# output
---
literal: |+
...
=head2 preserved_mapping, preserved_sequence
Since version 0.024
Experimental. Please report bugs or let me know this is useful and works.
With this you can define which nodes are dumped with the more compact flow
style instead of block style.
If you add C<PRESERVE_ORDER> to the C<preserve> option, it will also keep the
order of the keys in a hash.
use YAML::PP::Common qw/
PRESERVE_ORDER PRESERVE_FLOW_STYLE
YAML_FLOW_MAPPING_STYLE YAML_FLOW_SEQUENCE_STYLE
/;
my $yp = YAML::PP->new(
preserve => PRESERVE_FLOW_STYLE | PRESERVE_ORDER
);
my $hash = $yp->preserved_mapping({}, style => YAML_FLOW_MAPPING_STYLE);
# Add values after initialization to preserve order
%$hash = (z => 1, a => 2, y => 3, b => 4);
my $array = $yp->preserved_sequence([23, 24], style => YAML_FLOW_SEQUENCE_STYLE);
my $data = $yp->preserved_mapping({});
%$data = ( map => $hash, seq => $array );
my $dump = $yp->dump_string($data);
# output
---
map: {z: 1, a: 2, y: 3, b: 4}
seq: [23, 24]
=head2 loader
Returns or sets the loader object, by default L<YAML::PP::Loader>
=head2 dumper
Returns or sets the dumper object, by default L<YAML::PP::Dumper>
=head2 schema
Returns or sets the schema object
=head2 default_schema
Creates and returns the default schema
=head1 FUNCTIONS
The functions C<Load>, C<LoadFile>, C<Dump> and C<DumpFile> are provided
as a drop-in replacement for other existing YAML processors.
No function is exported by default.
Note that in scalar context, C<Load> and C<LoadFile> return the first
document (like L<YAML::Syck>), while L<YAML> and L<YAML::XS> return the
last.
=over
=item Load
use YAML::PP qw/ Load /;
my $doc = Load($yaml);
my @docs = Load($yaml);
Works like C<load_string>.
=item LoadFile
use YAML::PP qw/ LoadFile /;
my $doc = LoadFile($file);
my @docs = LoadFile($file);
my @docs = LoadFile($filehandle);
Works like C<load_file>.
=item Dump
use YAML::PP qw/ Dump /;
my $yaml = Dump($doc);
my $yaml = Dump(@docs);
Works like C<dump_string>.
=item DumpFile
use YAML::PP qw/ DumpFile /;
DumpFile($file, $doc);
DumpFile($file, @docs);
DumpFile($filehandle, @docs);
Works like C<dump_file>.