2
YAML::Tiny - Read/Write YAML files with as little code as possible
5
The YAML specification is huge. Really, really huge. It contains all the
6
functionality of XML, except with flexibility and choice, which makes it
7
easier to read, but with a formal specification that is more complex
10
The original pure-Perl implementation YAML costs just over 4 megabytes
11
of memory to load. Just like with Windows .ini files (3 meg to load) and
12
CSS (3.5 meg to load) the situation is just asking for a YAML::Tiny
13
module, an incomplete but correct and usable subset of the
14
functionality, in as little code as possible.
16
Like the other "::Tiny" modules, YAML::Tiny will have no non-core
17
dependencies, not require a compiler, and be back-compatible to at least
18
perl 5.005_03, and ideally 5.004.
21
#############################################
34
#############################################
40
my $yaml = YAML::Tiny->new;
43
$yaml = YAML::Tiny->read( 'file.yml' );
46
my $root = $yaml->[0]->{rootproperty};
47
my $one = $yaml->[0]->{section}->{one};
48
my $Foo = $yaml->[0]->{section}->{Foo};
51
$yaml->[0]->{newsection} = { this => 'that' }; # Add a section
52
$yaml->[0]->{section}->{Foo} = 'Not Bar!'; # Change a value
53
delete $yaml->[0]->{section}; # Delete a value or section
55
# Add an entire document
56
$yaml->[1] = [ 'foo', 'bar', 'baz' ];
59
$yaml->write( 'file.conf' );
62
YAML::Tiny is a perl class for reading and writing YAML-style files,
63
written with as little code as possible, reducing load time and memory
66
Most of the time it is accepted that Perl applications use a lot of
67
memory and modules. The ::Tiny family of modules is specifically
68
intended to provide an ultralight and zero-dependency alternative to
69
many more-thorough standard modules.
71
This module is primarily for reading human-written files (like simple
72
config files) and generating very simple human-readable files. Note that
73
I said human-readable and not geek-readable. The sort of files that your
74
average manager or secretary should be able to look at and make sense
77
YAML::Tiny does not generate comments, it won't necesarily preserve the
78
order of your hashes, and it will normalise if reading in and writing
81
It only supports a very basic subset of the full YAML specification.
83
Usage is targetted at files like Perl's META.yml, for which a small and
84
easily-embeddable module is extremely attractive.
86
Features will only be added if they are human readable, and can be
87
written in a few lines of code. Please don't be offended if your request
88
is refused. Someone has to draw the line, and for YAML::Tiny that
91
If you need something with more power move up to YAML (4 megabytes of
92
memory overhead) or YAML::Syck (275k, but requires libsyck and a C
95
To restate, YAML::Tiny does not preserve your comments, whitespace, or
96
the order of your YAML data. But it should round-trip from Perl
97
structure to file and back again just fine.
99
YAML TINY SPECIFICATION
100
This section of the documentation provides a specification for "YAML
101
Tiny", a subset of the YAML specification.
103
It is based on and described comparatively to the YAML 1.1 Working Draft
104
2004-12-28 specification, located at
105
<http://yaml.org/spec/current.html>.
107
Terminology and chapter numbers are based on that specification.
109
1. Introduction and Goals
110
The purpose of the YAML Tiny specification is to describe a useful
111
subset of the YAML specification that can be used for typical
112
document-oriented uses such as configuration files and simple data
115
Many specification elements that add flexibility or extensibility are
116
intentionally removed, as is support for complex datastructures, class
117
and object-orientation.
119
In general, YAML Tiny targets only those data structures available in
120
JSON, with the additional limitation that only simple keys are
123
As a result, all possible YAML Tiny documents should be able to be
124
transformed into an equivalent JSON document, although the reverse is
125
not necesarily true (but will be true in simple cases).
127
As a result of these simplifications the YAML Tiny specification should
128
be implementable in a relatively small amount of code in any language
129
that supports Perl Compatible Regular Expressions (PCRE).
132
YAML Tiny supports three data structures. These are scalars (in a
133
variety of forms), block-form sequences and block-form mappings.
134
Flow-style sequences and mappings are not supported, with some minor
135
exceptions detailed later.
137
The use of three dashes "---" to indicate the start of a new document is
138
supported, and multiple documents per file/stream is allowed.
140
Both line and inline comments are supported.
142
Scalars are supported via the plain style, single quote and double
143
quote, as well as literal-style and folded-style multi-line scalars.
145
The use of explicit tags is not supported.
147
The use of "null" type scalars is supported via the ~ character.
149
The use of "bool" type scalars is not supported.
151
However, serializer implementations should take care to explicitly
152
escape strings that match a "bool" keyword in the following set to
153
prevent other implementations that do support "bool" accidentally
154
reading a string as a boolean
156
y|Y|yes|Yes|YES|n|N|no|No|NO
157
|true|True|TRUE|false|False|FALSE
158
|on|On|ON|off|Off|OFF
160
The use of anchors and aliases is not supported.
162
The use of directives is supported only for the %YAML directive.
164
3. Processing YAML Tiny Information
167
The YAML specification dictates three-phase serialization and
168
three-phase deserialization.
170
The YAML Tiny specification does not mandate any particular methodology
171
or mechanism for parsing.
173
Any compliant parser is only required to parse a single document at a
174
time. The ability to support streaming documents is optional and most
177
Because anchors and aliases are not supported, the resulting
178
representation graph is thus directed but (unlike the main YAML
179
specification) acyclic.
181
Circular references/pointers are not possible, and any YAML Tiny
182
serializer detecting a circular reference should error with an
187
YAML Tiny is notionally unicode, but support for unicode is required if
188
the underlying language or system being used to implement a parser does
189
not support Unicode. If unicode is encountered in this case an error
192
Loading Failure Points
194
YAML Tiny parsers and emitters are not expected to recover from adapt to
195
errors. The specific error modality of any implementation is not
196
dictated (return codes, exceptions, etc) but is expected to be
202
YAML Tiny streams are implemented primarily using the ASCII character
203
set, although the use of Unicode inside strings is allowed if support by
206
Specific YAML Tiny encoded document types aiming for maximum
207
compatibility should restrict themselves to ASCII.
209
The escaping and unescaping of the 8-bit YAML escapes is required.
211
The escaping and unescaping of 16-bit and 32-bit YAML escapes is not
216
Support for the "~" null/undefined indicator is required.
218
Implementations may represent this as appropriate for the underlying
221
Support for the "-" block sequence indicator is required.
223
Support for the "?" mapping key indicator is not required.
225
Support for the ":" mapping value indicator is required.
227
Support for the "," flow collection indicator is not required.
229
Support for the "[" flow sequence indicator is not required, with one
230
exception (detailed below).
232
Support for the "]" flow sequence indicator is not required, with one
233
exception (detailed below).
235
Support for the "{" flow mapping indicator is not required, with one
236
exception (detailed below).
238
Support for the "}" flow mapping indicator is not required, with one
239
exception (detailed below).
241
Support for the "#" comment indicator is required.
243
Support for the "&" anchor indicator is not required.
245
Support for the "*" alias indicator is not required.
247
Support for the "!" tag indicator is not required.
249
Support for the "|" literal block indicator is required.
251
Support for the ">" folded block indicator is required.
253
Support for the "'" single quote indicator is required.
255
Support for the """ double quote indicator is required.
257
Support for the "%" directive indicator is required, but only for the
258
special case of a %YAML version directive before the "---" document
259
header, or on the same line as the document header.
265
- A sequence with a single element
269
To provide the ability to support empty sequences and mappings, support
270
for the constructs [] (empty sequence) and {} (empty mapping) are
276
# A document consisting of only an empty mapping
278
# A document consisting of only an empty sequence
280
# A document consisting of an empty mapping within a sequence
287
Other than the empty sequence and mapping cases described above, YAML
288
Tiny supports only the indentation-based block-style group of contexts.
290
All five scalar contexts are supported.
292
Indentation spaces work as per the YAML specification in all cases.
294
Comments work as per the YAML specification in all simple cases. Support
295
for indented multi-line comments is not required.
297
Seperation spaces work as per the YAML specification in all cases.
299
YAML Tiny Character Stream
301
The only directive supported by the YAML Tiny specification is the %YAML
302
language/version identifier. Although detected, this directive will have
303
no control over the parsing itself.
305
The parser must recognise both the YAML 1.0 and YAML 1.1+ formatting of
306
this directive (as well as the commented form, although no explicit code
307
should be needed to deal with this case, being a comment anyway)
309
That is, all of the following should be supported.
322
Support for the %TAG directive is not required.
324
Support for additional directives is not required.
326
Support for the document boundary marker "---" is required.
328
Support for the document boundary market "..." is not required.
330
If necesary, a document boundary should simply by indicated with a "---"
331
marker, with not preceding "..." marker.
333
Support for empty streams (containing no documents) is required.
335
Support for implicit document starts is required.
337
That is, the following must be equivalent.
349
Support for nodes optional anchor and tag properties are not required.
351
Support for node anchors is not required.
353
Support for node tags is not required.
355
Support for alias nodes is not required.
357
Support for flow nodes is not required.
359
Support for block nodes is required.
363
Support for all five scalar styles are required as per the YAML
364
specification, although support for quoted scalars spanning more than
365
one line is not required.
367
Support for the chomping indicators on multi-line scalar styles is
372
Support for block-style sequences is required.
374
Support for flow-style sequences is not required.
376
Support for block-style mappings is required.
378
Support for flow-style mappings is not required.
380
Both sequences and mappings should be able to be arbitrarily nested.
382
Support for plain-style mapping keys is required.
384
Support for quoted keys in mappings is not required.
386
Support for "?"-indicated explicit keys is not required.
388
Here endeth the specification.
390
Additional Perl-Specific Notes
391
For some Perl applications, it's important to know if you really have a
392
number and not a string.
394
That is, in some contexts is important that 3 the number is distinctive
397
Because even Perl itself is not trivially able to understand the
398
difference (certainly without XS-based modules) Perl implementations of
399
the YAML Tiny specification are not required to retain the
400
distinctiveness of 3 vs "3".
404
The constructor "new" creates and returns an empty "YAML::Tiny" object.
407
The "read" constructor reads a YAML file, and returns a new "YAML::Tiny"
408
object containing the contents of the file.
410
Returns the object on success, or "undef" on error.
412
When "read" fails, "YAML::Tiny" sets an error message internally you can
413
recover via "YAML::Tiny->errstr". Although in some cases a failed "read"
414
will also set the operating system error variable $!, not all errors do
415
and you should not rely on using the $! variable.
418
The "read_string" method takes as argument the contents of a YAML file
419
(a YAML document) as a string and returns the "YAML::Tiny" object for
423
The "write" method generates the file content for the properties, and
424
writes it to disk to the filename specified.
426
Returns true on success or "undef" on error.
429
Generates the file content for the object and returns it as a string.
432
When an error occurs, you can retrieve the error message either from the
433
$YAML::Tiny::errstr variable, or using the "errstr()" method.
436
YAML::Tiny implements a number of functions to add compatibility with
437
the YAML API. These should be a drop-in replacement, except that
438
YAML::Tiny will not export functions by default, and so you will need to
439
explicitly import the functions.
442
my $string = Dump(list-of-Perl-data-structures);
444
Turn Perl data into YAML. This function works very much like
445
Data::Dumper::Dumper().
447
It takes a list of Perl data strucures and dumps them into a serialized
450
It returns a string containing the YAML stream.
452
The structures can be references or plain scalars.
455
my @documents = Load(string-containing-a-YAML-stream);
457
Turn YAML into Perl data. This is the opposite of Dump.
459
Just like Storable's thaw() function or the eval() function in relation
462
It parses a string containing a valid YAML stream into a list of Perl
466
Aliases to Dump() and Load() for Storable fans. This will also allow
467
YAML::Tiny to be plugged directly into modules like POE.pm, that use the
468
freeze/thaw API for internal serialization.
470
DumpFile(filepath, list)
471
Writes the YAML stream to a file instead of just returning a string.
474
Reads the YAML stream from a file instead of a string.
477
Bugs should be reported via the CPAN bug tracker at
479
<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=YAML-Tiny>
482
Adam Kennedy <adamk@cpan.org>
485
YAML, YAML::Syck, Config::Tiny, CSS::Tiny,
486
<http://use.perl.org/~Alias/journal/29427>, <http://ali.as/>
489
Copyright 2006 - 2010 Adam Kennedy.
491
This program is free software; you can redistribute it and/or modify it
492
under the same terms as Perl itself.
494
The full text of the license can be found in the LICENSE file included