1YAML::PP::Schema::Perl(U3s)er Contributed Perl DocumentatYiAoMnL::PP::Schema::Perl(3)
2
3
4

NAME

6       YAML::PP::Schema::Perl - Schema for serializing perl objects and
7       special types
8

SYNOPSIS

10           use YAML::PP;
11           # This can be dangerous when loading untrusted YAML!
12           my $yp = YAML::PP->new( schema => [qw/ + Perl /] );
13           # or
14           my $yp = YAML::PP->new( schema => [qw/ Core Perl /] );
15           my $yaml = $yp->dump_string(sub { return 23 });
16
17           # loading code references
18           # This is very dangerous when loading untrusted YAML!!
19           my $yp = YAML::PP->new( schema => [qw/ + Perl +loadcode /] );
20           my $code = $yp->load_string(<<'EOM');
21           --- !perl/code |
22               {
23                   use 5.010;
24                   my ($name) = @_;
25                   say "Hello $name!";
26               }
27           EOM
28           $code->("Ingy");
29

DESCRIPTION

31       This schema allows you to load and dump perl objects and special types.
32
33       Please note that loading objects of arbitrary classes can be dangerous
34       in Perl. You have to load the modules yourself, but if an exploitable
35       module is loaded and an object is created, its "DESTROY" method will be
36       called when the object falls out of scope. File::Temp is an example
37       that can be exploitable and might remove arbitrary files.
38
39       Typeglobs are not implemented yet. Dumping code references is on by
40       default, but not loading (because that is easily exploitable since it's
41       using string "eval").
42
43   Tag Styles
44       You can define the style of tags you want to support:
45
46           my $yp_perl_two_one = YAML::PP->new(
47               schema => [qw/ + Perl tags=!!perl+!perl /],
48           );
49
50       "!perl" (default)
51           Only "!perl/type" tags are supported.
52
53       "!!perl"
54           Only "!!perl/type" tags are supported.
55
56       "!perl+!!perl"
57           Both "!perl/type" and "!!perl/tag" are supported when loading. When
58           dumping, "!perl/type" is used.
59
60       "!!perl+!perl"
61           Both "!perl/type" and "!!perl/tag" are supported when loading. When
62           dumping, "!!perl/type" is used.
63
64       YAML.pm, YAML::Syck and YAML::XS are using "!!perl/type" when dumping.
65
66       YAML.pm and YAML::Syck are supporting both "!perl/type" and
67       "!!perl/type" when loading. YAML::XS currently only supports the
68       latter.
69
70   Allow only certain classes
71       Since v0.017
72
73       Blessing arbitrary objects can be dangerous.  Maybe you want to allow
74       blessing only specific classes and ignore others.  For this you have to
75       instantiate a Perl Schema object first and use the "classes" option.
76
77       Currently it only allows a list of strings:
78
79           my $perl = YAML::PP::Schema::Perl->new(
80               classes => ['Foo', 'Bar'],
81           );
82           my $yp = YAML::PP::Perl->new(
83               schema => [qw/ + /, $perl],
84           );
85
86       Allowed classes will be loaded and dumped as usual. The others will be
87       ignored.
88
89       If you want to allow no objects at all, pass an empty array ref.
90
91   EXAMPLES
92       This is a list of the currently supported types and how they are dumped
93       into YAML:
94
95       array
96                   # Code
97                   [
98                       qw/ one two three four /
99                   ]
100
101
102                   # YAML
103                   ---
104                   - one
105                   - two
106                   - three
107                   - four
108
109       array_blessed
110                   # Code
111                   bless [
112                       qw/ one two three four /
113                   ], "Just::An::Arrayref"
114
115
116                   # YAML
117                   --- !perl/array:Just::An::Arrayref
118                   - one
119                   - two
120                   - three
121                   - four
122
123       circular
124                   # Code
125                   my $circle = bless [ 1, 2 ], 'Circle';
126                   push @$circle, $circle;
127                   $circle;
128
129
130                   # YAML
131                   --- &1 !perl/array:Circle
132                   - 1
133                   - 2
134                   - *1
135
136       coderef
137                   # Code
138                   sub {
139                       my (%args) = @_;
140                       return $args{x} + $args{y};
141                   }
142
143
144                   # YAML
145                   --- !perl/code |-
146                     {
147                         use warnings;
148                         use strict;
149                         (my(%args) = @_);
150                         (return ($args{'x'} + $args{'y'}));
151                     }
152
153       coderef_blessed
154                   # Code
155                   bless sub {
156                       my (%args) = @_;
157                       return $args{x} - $args{y};
158                   }, "I::Am::Code"
159
160
161                   # YAML
162                   --- !perl/code:I::Am::Code |-
163                     {
164                         use warnings;
165                         use strict;
166                         (my(%args) = @_);
167                         (return ($args{'x'} - $args{'y'}));
168                     }
169
170       hash
171                   # Code
172                   {
173                       U => 2,
174                       B => 52,
175                   }
176
177
178                   # YAML
179                   ---
180                   B: 52
181                   U: 2
182
183       hash_blessed
184                   # Code
185                   bless {
186                       U => 2,
187                       B => 52,
188                   }, 'A::Very::Exclusive::Class'
189
190
191                   # YAML
192                   --- !perl/hash:A::Very::Exclusive::Class
193                   B: 52
194                   U: 2
195
196       refref
197                   # Code
198                   my $ref = { a => 'hash' };
199                   my $refref = \$ref;
200                   $refref;
201
202
203                   # YAML
204                   --- !perl/ref
205                   =:
206                     a: hash
207
208       refref_blessed
209                   # Code
210                   my $ref = { a => 'hash' };
211                   my $refref = bless \$ref, 'Foo';
212                   $refref;
213
214
215                   # YAML
216                   --- !perl/ref:Foo
217                   =:
218                     a: hash
219
220       regexp
221                   # Code
222                   my $string = 'unblessed';
223                   qr{$string}
224
225
226                   # YAML
227                   --- !perl/regexp unblessed
228
229       regexp_blessed
230                   # Code
231                   my $string = 'blessed';
232                   bless qr{$string}, "Foo"
233
234
235                   # YAML
236                   --- !perl/regexp:Foo blessed
237
238       scalarref
239                   # Code
240                   my $scalar = "some string";
241                   my $scalarref = \$scalar;
242                   $scalarref;
243
244
245                   # YAML
246                   --- !perl/scalar
247                   =: some string
248
249       scalarref_blessed
250                   # Code
251                   my $scalar = "some other string";
252                   my $scalarref = bless \$scalar, 'Foo';
253                   $scalarref;
254
255
256                   # YAML
257                   --- !perl/scalar:Foo
258                   =: some other string
259
260   METHODS
261       new
262               my $perl = YAML::PP::Schema::Perl->new(
263                   tags => "!perl",
264                   classes => ['MyClass'],
265                   loadcode => 1,
266               );
267
268           The constructor recognizes the following options:
269
270           tags
271               Default: '"!perl"'
272
273               See "Tag Styles"
274
275           classes
276               Default: "undef"
277
278               Since: v0.017
279
280               Accepts an array ref of class names
281
282           loadcode
283               Default: 0
284
285       register
286           A class method called by YAML::PP::Schema
287
288       construct_ref, represent_ref
289           Perl variables of the type "REF" are represented in yaml like this:
290
291               --- !perl/ref
292               =:
293                 a: 1
294
295           "construct_ref" returns the perl data:
296
297               my $data = YAML::PP::Schema::Perl->construct_ref([ '=', { some => 'data' } );
298               my $data = \{ a => 1 };
299
300           "represent_ref" turns a "REF" variable into a YAML mapping:
301
302               my $data = YAML::PP::Schema::Perl->represent_ref(\{ a => 1 });
303               my $data = { '=' => { a => 1 } };
304
305       construct_scalar, represent_scalar
306           Perl variables of the type "SCALAR" are represented in yaml like
307           this:
308
309               --- !perl/scalar
310               =: string
311
312           "construct_scalar" returns the perl data:
313
314               my $data = YAML::PP::Schema::Perl->construct_ref([ '=', 'string' );
315               my $data = \'string';
316
317           "represent_scalar" turns a "SCALAR" variable into a YAML mapping:
318
319               my $data = YAML::PP::Schema::Perl->represent_scalar(\'string');
320               my $data = { '=' => 'string' };
321
322       construct_regex, represent_regex
323           "construct_regex" returns a "qr{}" object from the YAML string:
324
325               my $qr = YAML::PP::Schema::Perl->construct_regex('foo.*');
326
327           "represent_regex" returns a string representing the regex object:
328
329               my $string = YAML::PP::Schema::Perl->represent_regex(qr{...});
330
331       evaluate_code, represent_code
332           "evaluate_code" returns a code reference from a string. The string
333           must start with a "{" and end with a "}".
334
335               my $code = YAML::PP::Schema::Perl->evaluate_code('{ return 23 }');
336
337           "represent_code" returns a string representation of the code
338           reference with the help of B::Deparse:
339
340               my $string = YAML::PP::Schema::Perl->represent_code(sub { return 23 });
341
342       object
343           Does the same as "bless":
344
345               my $object = YAML::PP::Schema::Perl->object($data, $class);
346
347
348
349perl v5.30.1                      2020-03-02         YAML::PP::Schema::Perl(3)
Impressum