|
NAMEIO::YAML - read and write YAML streams incrementallySYNOPSISuse IO::YAML; $io = IO::YAML->new($path_or_filehandle); $io = IO::YAML->new( 'path' => '/path/to/a/file', 'auto_load' => $bool, ); $io = IO::YAML->new( 'handle' => $fh, 'mode' => '>', # or 'w'; '<' or 'r'; '>>' or 'a' ); $io = IO::YAML->new; $io->open($path, '>') or die $!; # Open a stream for writing $io->open($path, '>>') or die $!; # Open a stream for appending # Automatically add "...\n" at end of each document written $io->auto_terminate(1); print $io $mystring; print $io @myvalues; print $io \@myarray; print $io \%myhash; print $io $myobj; $io = IO::YAML->new; $io->open($path, '<') or die $!; # Open a stream for reading while (<$io>) { $data = YAML::Load($_); } $io = IO::YAML->new; $io->open($path) or die $!; # Default mode is reading $io->auto_load(1); while (not $io->eof) { $data = <$io>; } $io = IO::YAML->new($path_or_handle); $io->auto_load(1); my @values = <$io>; # Roughly equivalent to YAML::LoadFile(...) DESCRIPTIONIO::YAML may be used to read and write YAML streams one "document" (i.e., one value) at a time.A YAML stream is a file consisting of a sequence of YAML documents, each of which may (optionally) be terminated by a line consisting solely of three periods ("..."). The first line of each document must begin with the three-byte sequence "---". Here's a simple YAML file consisting of three documents; their values are the string 'foo', an empty array, and a hash with three elements: --- #YAML:1.0 foo --- #YAML:1.0 [] --- #YAML:1.0 title: Testing 1, 2, 3 author: nkuitse date: 2004-03-05 ^D (Here, "^D" indicates the end of the file.) In this next example, the stream consists of a single YAML document whose value is "undef": --- ~ ^D As this example shows, the first line in each document need not contain the full YAML 1.0 header. Reading from a YAML streamTo read from a YAML stream, you may use the angle-brackets operator (e.g., <$fh>) or the equivalent methods "getline" or "read". Rather than reading a single line, this will read an entire YAML document.while(defined(my $yaml = <$io>)) { my $value = YAML::Load($yaml); ... } The "YAML::Load" step may be omitted by setting the IO::YAML object's "auto_load" property to a true value: $io->auto_load(1); while(defined(my $value = <$io>)) { ... } However, this example is complicated by the fact that the value of a YAML document may be undef; the loop as written will terminate when the end of the stream is reached or when an undef value is read. To avoid this problem while still taking advantage of the "auto_load" property, use "$io->eof" to test for the end of the stream: $io->auto_load(1); while(not $io->eof) { my $value = <$io>; ... } IO::YAML properly recognizes the document terminator ("..."). Some versions of YAML do not recognize it, however; in order to prevent problems when reading YAML streams with auto-loading off, IO::YAML strips the document terminator line if it is present. Writing to a YAML streamTo print to a YAML stream, call "print" just as you would with a regular file handle; the value(s) you're printing will be converted to YAML format before being written:$io = IO::YAML->new; $io->open('>file') or die "Couldn't open 'file'"; print $io $anything; You can `print' anything that YAML is capable of serializing; an exception will be raised if you attempt to print something that can't be serialized (e.g., a reference to a subroutine). The complication with undef values that affects the reading of a YAML stream is not an issue when writing to a YAML stream. Terminating YAML documentsDocuments in a YAML stream may be terminated by a line consisting solely of the string "...". You can use the "terminate" method to add an explicit document terminator to a YAML stream that you have open for writing (or appending):$io = IO::YAML->new($file_or_handle, '>'); foreach my $value (@data_values) { print $io $value; $io->terminate; } It's generally safer to have YAML documents terminated automatically: # 1. Set auto_terminate to a true value # a) When creating the object $io = IO::YAML->new( 'handle' => $fh, 'mode' => '>>', 'auto_terminate' => 1, ); # or b) At any point thereafter $io = IO::YAML->new(...); $io->auto_terminate(1); # 2. Documents are now auto-terminated foreach my $value (@data_values) { print $io $value; # $io->terminate called implicitly } Note that it's not the YAML stream that's terminated; it's the YAML document that was previously written. Low-level accessSometimes it is helpful to be able to access a YAML stream at a lower level. For example, you may wish to read and write a file consisting of a YAML document (here, serving as a header of sorts) followed by arbitrary text. The "handle" method may be used to obtain the underlying file handle so that it can be used for this low-level access:# Read header + body $io->auto_load(1); $header = <$io>; $fh = $io->handle; while (<$fh>) { # Process each line after the YAML document } # Write header + body $io->auto_terminate(1); print $io $header; $fh = $io->handle; for (@other_stuff_to_write) { print $fh $_; } METHODS
NOTE: Numeric modes are not yet implemented.
BUGSAutoflush might not be working.Seeking to the first position beyond the end of the YAML stream should be possible but doesn't currently work. TO DONormalize modes passed in the constructor.Implement numeric modes. Add tests for seek and tell methods. Enable seeking to the first byte beyond the end of the YAML stream. Figure out how to allow read-write access and truncate(). SEE ALSOYAMLAUTHORPaul Hoffman (nkuitse AT cpan DOT org)COPYRIGHTCopyright 2004-2007, 2009 Paul M. Hoffman.This is free software, and is made available under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |