TAPx::Parser::YAML - Read/Write YAML files with as little code as possible
Version 0.50_07
############################################# # In your file --- rootproperty: blah section: one: two three: four Foo: Bar empty: ~ ############################################# # In your program use TAPx::Parser::YAML; # Create a YAML file my $yaml = TAPx::Parser::YAML->new; # Open the config $yaml = TAPx::Parser::YAML->read( 'file.yml' ); # Reading properties my $root = $yaml->[0]->{rootproperty}; my $one = $yaml->[0]->{section}->{one}; my $Foo = $yaml->[0]->{section}->{Foo}; # Changing data $yaml->[0]->{newsection} = { this => 'that' }; # Add a section $yaml->[0]->{section}->{Foo} = 'Not Bar!'; # Change a value delete $yaml->[0]->{section}; # Delete a value or section # Add an entire document $yaml->[1] = [ 'foo', 'bar', 'baz' ]; # Save the file $yaml->write( 'file.conf' );
Note that this code is lifted directly from YAML::Tiny and used with the permission of Adam Kennedy.
TAPx::Parser::YAML is a perl class to read and write YAML-style files with as little code as possible, reducing load time and memory overhead.
Most of the time it is accepted that Perl applications use a lot of memory and modules. The ::Tiny family of modules is specifically intended to provide an ultralight and zero-dependency alternative to the standard modules.
This module is primarily for reading human-written files (like config files) and generating very simple human-readable files. Note that I said human-readable and not geek-readable. The sort of files that your average manager or secretary should be able to look at and make sense of.
TAPx::Parser::YAML does not generate comments, it won't necesarily preserve the order of your hashes, and it will normalise if reading in and writing out again.
It only supports a very basic subset of the full YAML specification.
Usage is targetted at files like Perl's META.yml, for which a small and easily-embeddable module would be highly useful.
Features will only be added if they are human readable, and can be written in a few lines of code. Please don't be offended if your request is refused. Someone has to draw the line, and for TAPx::Parser::YAML that someone is me.
If you need something with more power move up to YAML (4 megabytes of memory overhead) or YAML::Syck (275k, but requires libsyck and a C compiler).
To restate, TAPx::Parser::YAML does not preserve your comments, whitespace, or the order of your YAML data. But it should round-trip from Perl structure to file and back again just fine.
The constructor new creates and returns an empty TAPx::Parser::YAML object.
new
TAPx::Parser::YAML
The read constructor reads a YAML file, and returns a new TAPx::Parser::YAML object containing the contents of the file.
read
Returns the object on success, or undef on error.
undef
When read fails, TAPx::Parser::YAML sets an error message internally you can recover via TAPx::Parser::YAML->errstr. Although in some cases a failed read will also set the operating system error variable $!, not all errors do and you should not rely on using the $! variable.
TAPx::Parser::YAML->errstr
$!
The read_string method takes as argument the contents of a YAML file (a YAML document) as a string and returns the TAPx::Parser::YAML object for it.
read_string
The write method generates the file content for the properties, and writes it to disk to the filename specified.
write
Returns true on success or undef on error.
Generates the file content for the object and returns it as a string.
When an error occurs, you can retrieve the error message either from the $TAPx::Parser::YAML::errstr variable, or using the errstr() method.
$TAPx::Parser::YAML::errstr
errstr()
Bugs should be reported via the CPAN bug tracker at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=YAML-Tiny
Adam Kennedy <adamk@cpan.org>
YAML, YAML::Syck, Config::Tiny, CSS::Tiny, http://use.perl.org/~Alias/journal/29427
Copyright 2006 - 2007 Adam Kennedy.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
To install TAPx::Parser, copy and paste the appropriate command in to your terminal.
cpanm
cpanm TAPx::Parser
CPAN shell
perl -MCPAN -e shell install TAPx::Parser
For more information on module installation, please visit the detailed CPAN module installation guide.