1YAML::Tiny(3) User Contributed Perl Documentation YAML::Tiny(3)
2
3
4
6 YAML::Tiny - Read/Write YAML files with as little code as possible
7
9 The YAML specification is huge. Really, really huge. It contains all
10 the functionality of XML, except with flexibility and choice, which
11 makes it easier to read, but with a formal specification that is more
12 complex than XML.
13
14 The original pure-Perl implementation YAML costs just over 4 megabytes
15 of memory to load. Just like with Windows .ini files (3 meg to load)
16 and CSS (3.5 meg to load) the situation is just asking for a YAML::Tiny
17 module, an incomplete but correct and usable subset of the
18 functionality, in as little code as possible.
19
20 Like the other "::Tiny" modules, YAML::Tiny will have no non-core
21 dependencies, not require a compiler, and be back-compatible to at
22 least perl 5.005_03, and ideally 5.004.
23
25 #############################################
26 # In your file
27
28 ---
29 rootproperty: blah
30 section:
31 one: two
32 three: four
33 Foo: Bar
34 empty: ~
35
36
37
38 #############################################
39 # In your program
40
41 use YAML::Tiny;
42
43 # Create a YAML file
44 my $yaml = YAML::Tiny->new;
45
46 # Open the config
47 $yaml = YAML::Tiny->read( 'file.yml' );
48
49 # Reading properties
50 my $root = $yaml->[0]->{rootproperty};
51 my $one = $yaml->[0]->{section}->{one};
52 my $Foo = $yaml->[0]->{section}->{Foo};
53
54 # Changing data
55 $yaml->[0]->{newsection} = { this => 'that' }; # Add a section
56 $yaml->[0]->{section}->{Foo} = 'Not Bar!'; # Change a value
57 delete $yaml->[0]->{section}; # Delete a value or section
58
59 # Add an entire document
60 $yaml->[1] = [ 'foo', 'bar', 'baz' ];
61
62 # Save the file
63 $yaml->write( 'file.conf' );
64
66 YAML::Tiny is a perl class for reading and writing YAML-style files,
67 written with as little code as possible, reducing load time and memory
68 overhead.
69
70 Most of the time it is accepted that Perl applications use a lot of
71 memory and modules. The ::Tiny family of modules is specifically
72 intended to provide an ultralight and zero-dependency alternative to
73 many more-thorough standard modules.
74
75 This module is primarily for reading human-written files (like simple
76 config files) and generating very simple human-readable files. Note
77 that I said human-readable and not geek-readable. The sort of files
78 that your average manager or secretary should be able to look at and
79 make sense of.
80
81 YAML::Tiny does not generate comments, it won't necesarily preserve the
82 order of your hashes, and it will normalise if reading in and writing
83 out again.
84
85 It only supports a very basic subset of the full YAML specification.
86
87 Usage is targetted at files like Perl's META.yml, for which a small and
88 easily-embeddable module is extremely attractive.
89
90 Features will only be added if they are human readable, and can be
91 written in a few lines of code. Please don't be offended if your
92 request is refused. Someone has to draw the line, and for YAML::Tiny
93 that someone is me.
94
95 If you need something with more power move up to YAML (4 megabytes of
96 memory overhead) or YAML::Syck (275k, but requires libsyck and a C
97 compiler).
98
99 To restate, YAML::Tiny does not preserve your comments, whitespace, or
100 the order of your YAML data. But it should round-trip from Perl
101 structure to file and back again just fine.
102
104 This section of the documentation provides a specification for "YAML
105 Tiny", a subset of the YAML specification.
106
107 It is based on and described comparatively to the YAML 1.1 Working
108 Draft 2004-12-28 specification, located at
109 <http://yaml.org/spec/current.html>.
110
111 Terminology and chapter numbers are based on that specification.
112
113 1. Introduction and Goals
114 The purpose of the YAML Tiny specification is to describe a useful
115 subset of the YAML specification that can be used for typical document-
116 oriented uses such as configuration files and simple data structure
117 dumps.
118
119 Many specification elements that add flexibility or extensibility are
120 intentionally removed, as is support for complex datastructures, class
121 and object-orientation.
122
123 In general, YAML Tiny targets only those data structures available in
124 JSON, with the additional limitation that only simple keys are
125 supported.
126
127 As a result, all possible YAML Tiny documents should be able to be
128 transformed into an equivalent JSON document, although the reverse is
129 not necesarily true (but will be true in simple cases).
130
131 As a result of these simplifications the YAML Tiny specification should
132 be implementable in a relatively small amount of code in any language
133 that supports Perl Compatible Regular Expressions (PCRE).
134
135 2. Introduction
136 YAML Tiny supports three data structures. These are scalars (in a
137 variety of forms), block-form sequences and block-form mappings. Flow-
138 style sequences and mappings are not supported, with some minor
139 exceptions detailed later.
140
141 The use of three dashes "---" to indicate the start of a new document
142 is supported, and multiple documents per file/stream is allowed.
143
144 Both line and inline comments are supported.
145
146 Scalars are supported via the plain style, single quote and double
147 quote, as well as literal-style and folded-style multi-line scalars.
148
149 The use of tags is not supported.
150
151 The use of anchors and aliases is not supported.
152
153 The use of directives is supported only for the %YAML directive.
154
155 3. Processing YAML Tiny Information
156 Processes
157
158 The YAML specification dictates three-phase serialization and three-
159 phase deserialization.
160
161 The YAML Tiny specification does not mandate any particular methodology
162 or mechanism for parsing.
163
164 Any compliant parser is only required to parse a single document at a
165 time. The ability to support streaming documents is optional and most
166 likely non-typical.
167
168 Because anchors and aliases are not supported, the resulting
169 representation graph is thus directed but (unlike the main YAML
170 specification) acyclic.
171
172 Circular references/pointers are not possible, and any YAML Tiny
173 serializer detecting a circulars should error with an appropriate
174 message.
175
176 Presentation Stream
177
178 YAML Tiny is notionally unicode, but support for unicode is required if
179 the underlying language or system being used to implement a parser does
180 not support Unicode. If unicode is encountered in this case an error
181 should be returned.
182
183 Loading Failure Points
184
185 YAML Tiny parsers and emitters are not expected to recover from adapt
186 to errors. The specific error modality of any implementation is not
187 dictated (return codes, exceptions, etc) but is expected to be
188 consistant.
189
190 4. Syntax
191 Character Set
192
193 YAML Tiny streams are implemented primarily using the ASCII character
194 set, although the use of Unicode inside strings is allowed if support
195 by the implementation.
196
197 Specific YAML Tiny encoded document types aiming for maximum
198 compatibility should restrict themselves to ASCII.
199
200 The escaping and unescaping of the 8-bit YAML escapes is required.
201
202 The escaping and unescaping of 16-bit and 32-bit YAML escapes is not
203 required.
204
205 Indicator Characters
206
207 Support for the "~" null/undefined indicator is required.
208
209 Implementations may represent this as appropriate for the underlying
210 language.
211
212 Support for the "-" block sequence indicator is required.
213
214 Support for the "?" mapping key indicator is not required.
215
216 Support for the ":" mapping value indicator is required.
217
218 Support for the "," flow collection indicator is not required.
219
220 Support for the "[" flow sequence indicator is not required, with one
221 exception (detailed below).
222
223 Support for the "]" flow sequence indicator is not required, with one
224 exception (detailed below).
225
226 Support for the "{" flow mapping indicator is not required, with one
227 exception (detailed below).
228
229 Support for the "}" flow mapping indicator is not required, with one
230 exception (detailed below).
231
232 Support for the "#" comment indicator is required.
233
234 Support for the "&" anchor indicator is not required.
235
236 Support for the "*" alias indicator is not required.
237
238 Support for the "!" tag indicator is not required.
239
240 Support for the "|" literal block indicator is required.
241
242 Support for the ">" folded block indicator is required.
243
244 Support for the "'" single quote indicator is required.
245
246 Support for the """ double quote indicator is required.
247
248 Support for the "%" directive indicator is required, but only for the
249 special case of a %YAML version directive before the "---" document
250 header, or on the same line as the document header.
251
252 For example:
253
254 %YAML 1.1
255 ---
256 - A sequence with a single element
257
258 Special Exception:
259
260 To provide the ability to support empty sequences and mappings, support
261 for the constructs [] (empty sequence) and {} (empty mapping) are
262 required.
263
264 For example,
265
266 %YAML 1.1
267 # A document consisting of only an empty mapping
268 --- {}
269 # A document consisting of only an empty sequence
270 --- []
271 # A document consisting of an empty mapping within a sequence
272 - foo
273 - {}
274 - bar
275
276 Syntax Primitives
277
278 Other than the empty sequence and mapping cases described above, YAML
279 Tiny supports only the indentation-based block-style group of contexts.
280
281 All five scalar contexts are supported.
282
283 Indentation spaces work as per the YAML specification in all cases.
284
285 Comments work as per the YAML specification in all simple cases.
286 Support for indented multi-line comments is not required.
287
288 Seperation spaces work as per the YAML specification in all cases.
289
290 YAML Tiny Character Stream
291
292 The only directive supported by the YAML Tiny specification is the
293 %YAML language/version identifier. Although detected, this directive
294 will have no control over the parsing itself.
295
296 The parser must recognise both the YAML 1.0 and YAML 1.1+ formatting of
297 this directive (as well as the commented form, although no explicit
298 code should be needed to deal with this case, being a comment anyway)
299
300 That is, all of the following should be supported.
301
302 --- #YAML:1.0
303 - foo
304
305 %YAML:1.0
306 ---
307 - foo
308
309 % YAML 1.1
310 ---
311 - foo
312
313 Support for the %TAG directive is not required.
314
315 Support for additional directives is not required.
316
317 Support for the document boundary marker "---" is required.
318
319 Support for the document boundary market "..." is not required.
320
321 If necesary, a document boundary should simply by indicated with a
322 "---" marker, with not preceding "..." marker.
323
324 Support for empty streams (containing no documents) is required.
325
326 Support for implicit document starts is required.
327
328 That is, the following must be equivalent.
329
330 # Full form
331 %YAML 1.1
332 ---
333 foo: bar
334
335 # Implicit form
336 foo: bar
337
338 Nodes
339
340 Support for nodes optional anchor and tag properties are not required.
341
342 Support for node anchors is not required.
343
344 Supprot for node tags is not required.
345
346 Support for alias nodes is not required.
347
348 Support for flow nodes is not required.
349
350 Support for block nodes is required.
351
352 Scalar Styles
353
354 Support for all five scalar styles are required as per the YAML
355 specification, although support for quoted scalars spanning more than
356 one line is not required.
357
358 Support for the chomping indicators on multi-line scalar styles is
359 required.
360
361 Collection Styles
362
363 Support for block-style sequences is required.
364
365 Support for flow-style sequences is not required.
366
367 Support for block-style mappings is required.
368
369 Support for flow-style mappings is not required.
370
371 Both sequences and mappings should be able to be arbitrarily nested.
372
373 Support for plain-style mapping keys is required.
374
375 Support for quoted keys in mappings is not required.
376
377 Support for "?"-indicated explicit keys is not required.
378
379 Here endeth the specification.
380
381 Additional Perl-Specific Notes
382 For some Perl applications, it's important to know if you really have a
383 number and not a string.
384
385 That is, in some contexts is important that 3 the number is distinctive
386 from "3" the string.
387
388 Because even Perl itself is not trivially able to understand the
389 difference (certainly without XS-based modules) Perl implementations of
390 the YAML Tiny specification are not required to retain the
391 distinctiveness of 3 vs "3".
392
394 new
395 The constructor "new" creates and returns an empty "YAML::Tiny" object.
396
397 read $filename
398 The "read" constructor reads a YAML file, and returns a new
399 "YAML::Tiny" object containing the contents of the file.
400
401 Returns the object on success, or "undef" on error.
402
403 When "read" fails, "YAML::Tiny" sets an error message internally you
404 can recover via "YAML::Tiny->errstr". Although in some cases a failed
405 "read" will also set the operating system error variable $!, not all
406 errors do and you should not rely on using the $! variable.
407
408 read_string $string;
409 The "read_string" method takes as argument the contents of a YAML file
410 (a YAML document) as a string and returns the "YAML::Tiny" object for
411 it.
412
413 write $filename
414 The "write" method generates the file content for the properties, and
415 writes it to disk to the filename specified.
416
417 Returns true on success or "undef" on error.
418
419 write_string
420 Generates the file content for the object and returns it as a string.
421
422 errstr
423 When an error occurs, you can retrieve the error message either from
424 the $YAML::Tiny::errstr variable, or using the "errstr()" method.
425
427 YAML::Tiny implements a number of functions to add compatibility with
428 the YAML API. These should be a drop-in replacement, except that
429 YAML::Tiny will not export functions by default, and so you will need
430 to explicitly import the functions.
431
432 Dump
433 my $string = Dump(list-of-Perl-data-structures);
434
435 Turn Perl data into YAML. This function works very much like
436 Data::Dumper::Dumper().
437
438 It takes a list of Perl data strucures and dumps them into a serialized
439 form.
440
441 It returns a string containing the YAML stream.
442
443 The structures can be references or plain scalars.
444
445 Load
446 my @documents = Load(string-containing-a-YAML-stream);
447
448 Turn YAML into Perl data. This is the opposite of Dump.
449
450 Just like Storable's thaw() function or the eval() function in relation
451 to Data::Dumper.
452
453 It parses a string containing a valid YAML stream into a list of Perl
454 data structures.
455
456 freeze() and thaw()
457 Aliases to Dump() and Load() for Storable fans. This will also allow
458 YAML::Tiny to be plugged directly into modules like POE.pm, that use
459 the freeze/thaw API for internal serialization.
460
461 DumpFile(filepath, list)
462 Writes the YAML stream to a file instead of just returning a string.
463
464 LoadFile(filepath)
465 Reads the YAML stream from a file instead of a string.
466
468 Bugs should be reported via the CPAN bug tracker at
469
470 http://rt.cpan.org/NoAuth/ReportBug.html?Queue=YAML-Tiny
471 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=YAML-Tiny>
472
474 Adam Kennedy <adamk@cpan.org>
475
477 YAML, YAML::Syck, Config::Tiny, CSS::Tiny,
478 <http://use.perl.org/~Alias/journal/29427>, <http://ali.as/>
479
481 Copyright 2006 - 2009 Adam Kennedy.
482
483 This program is free software; you can redistribute it and/or modify it
484 under the same terms as Perl itself.
485
486 The full text of the license can be found in the LICENSE file included
487 with this module.
488
489
490
491perl v5.12.0 2009-07-31 YAML::Tiny(3)