Data-YAML-0.0.6/0000755000076500001200000000000011154603761011732 5ustar andyadminData-YAML-0.0.6/Changes0000644000076500001200000000042111154603700013213 0ustar andyadminRevision history for Data-YAML 0.0.3 2007-03-17 - Initial release. 0.0.4 2007-03-17 - Added tests for input / output modes - Added the tests to MANIFEST. D'oh. 0.0.5 2007-03-20 0.0.6 2009-03-07 - Fix ESCAPE_CHAR regex. Refs #31804. Data-YAML-0.0.6/MANIFEST0000644000076500001200000000054311154603756013071 0ustar andyadminChanges lib/Data/YAML.pm lib/Data/YAML/Reader.pm lib/Data/YAML/Writer.pm Makefile.PL MANIFEST README t/00.load.t t/10-input.t t/20-reader.t t/30-output.t t/40-writer.t t/pod-coverage.t t/pod.t META.yml Module meta-data (added by MakeMaker) SIGNATURE Public-key signature (added by MakeMaker) Data-YAML-0.0.6/META.yml0000644000076500001200000000103011154603756013201 0ustar andyadmin--- #YAML:1.0 name: Data-YAML version: 0.0.6 abstract: Easy YAML serialisation of Perl data structures author: - Andy Armstrong license: perl distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 requires: Test::More: 0 version: 0 no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.48 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 Data-YAML-0.0.6/Makefile.PL0000644000076500001200000000116211154603246013702 0ustar andyadminuse strict; use warnings; use ExtUtils::MakeMaker; eval 'use ExtUtils::MakeMaker::Coverage'; warn "Optional ExtUtils::MakeMaker::Coverage not available\n" if $@; WriteMakefile( ( MM->can( 'signature_target' ) ? ( SIGN => 1 ) : () ), NAME => 'Data::YAML', AUTHOR => 'Andy Armstrong ', LICENSE => 'perl', VERSION_FROM => 'lib/Data/YAML.pm', ABSTRACT_FROM => 'lib/Data/YAML.pm', PL_FILES => {}, PREREQ_PM => { 'Test::More' => 0, 'version' => 0, }, dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, clean => { FILES => 'Data-YAML-*' }, ); Data-YAML-0.0.6/README0000644000076500001200000000053011154603403012601 0ustar andyadminData-YAML version 0.0.6 INSTALLATION To install this module, run the following commands: perl Makefile.PL make make test make install DEPENDENCIES None. COPYRIGHT AND LICENCE Copyright (C) 2007, Andy Armstrong This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Data-YAML-0.0.6/SIGNATURE0000644000076500001200000000312411154603761013216 0ustar andyadminThis file contains message digests of all files listed in MANIFEST, signed via the Module::Signature module, version 0.55. To verify the content in this distribution, first make sure you have Module::Signature installed, then type: % cpansign -v It will check each file's integrity, as well as the signature's validity. If "==> Signature verified OK! <==" is not displayed, the distribution may already have been compromised, and you should not run its Makefile.PL or Build.PL. -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 SHA1 a796da56d11015113d6690491bd352c9958c5c64 Changes SHA1 1f4a6f38328ce9be2882634c0bdc84d6d9cb98e4 MANIFEST SHA1 06f48c95ab84fcd8dd0e1a92e45c48dd8b65395e META.yml SHA1 6a0c4a0f0093335e6e1e8f7d6fd1fcecad0c4a83 Makefile.PL SHA1 0aa5814a1f2c123d5b0f51643a1eff297543e589 README SHA1 446ba3a109b32a9091b4313fb1b70bb1e704ed76 lib/Data/YAML.pm SHA1 32611505ba10508d42104ee676466edd0f7ffa89 lib/Data/YAML/Reader.pm SHA1 ae4f9923febd88025bb2f923b1f6b6e8ec5e9982 lib/Data/YAML/Writer.pm SHA1 4cf9e6d03b2ba3e3ab482d138a94e95c6ecf6492 t/00.load.t SHA1 161ecef48d8e921399c3406cc2b2ca85442bd0e7 t/10-input.t SHA1 e3de4eff0004dcaf16aaed29a3df478f50a7058c t/20-reader.t SHA1 ae69251cc811bc4934ad08c165f34be398474e4f t/30-output.t SHA1 9b7b9be6fd31c4b378dd4b1a866cb68a51b1030c t/40-writer.t SHA1 aeaa691851a425f07ab6a8d4f825e2ad9d92b8ee t/pod-coverage.t SHA1 0190346d7072d458c8a10a45c19f86db641dcc48 t/pod.t -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (Darwin) iEYEARECAAYFAkmzB+4ACgkQwoknRJZQnCEljwCgiQmQmQlvYQOTc118yk1GxyRk O2IAoMAritfzopChUsyF9HyFXCFUrfLl =o0s7 -----END PGP SIGNATURE----- Data-YAML-0.0.6/lib/0000755000076500001200000000000011154603756012504 5ustar andyadminData-YAML-0.0.6/lib/Data/0000755000076500001200000000000011154603756013355 5ustar andyadminData-YAML-0.0.6/lib/Data/YAML/0000755000076500001200000000000011154603756014117 5ustar andyadminData-YAML-0.0.6/lib/Data/YAML/Reader.pm0000644000076500001200000002360011154603403015645 0ustar andyadminpackage Data::YAML::Reader; use strict; use warnings; use Carp; use vars qw{$VERSION}; $VERSION = '0.0.6'; # TODO: # Handle blessed object syntax # Printable characters for escapes my %UNESCAPES = ( z => "\x00", a => "\x07", t => "\x09", n => "\x0a", v => "\x0b", f => "\x0c", r => "\x0d", e => "\x1b", '\\' => '\\', ); my $QQ_STRING = qr{ " (?:\\. | [^"])* " }x; my $HASH_LINE = qr{ ^ ($QQ_STRING|\S+) \s* : (?: \s+ (.+?) \s* )? $ }x; my $IS_HASH_KEY = qr{ ^ [\w\'\"] }x; my $IS_END_YAML = qr{ ^ [.][.][.] \s* $ }x; my $IS_QQ_STRING = qr{ ^ $QQ_STRING $ }x; # Create an empty Data::YAML::Reader object sub new { my $class = shift; bless {}, $class; } sub _make_reader { my $self = shift; my $obj = shift; croak "Must have something to read from" unless defined $obj; if ( my $ref = ref $obj ) { if ( 'CODE' eq $ref ) { return $obj; } elsif ( 'ARRAY' eq $ref ) { return sub { shift @$obj }; } elsif ( 'SCALAR' eq $ref ) { return $self->_make_reader( $$obj ); } elsif ( 'GLOB' eq $ref || 'IO::Handle' eq $ref ) { return sub { my $line = <$obj>; chomp $line if defined $line; return $line; }; } croak "Don't know how to read $ref"; } else { my @lines = split( /\n/, $obj ); return sub { shift @lines }; } } sub read { my $self = shift; my $obj = shift; $self->{reader} = $self->_make_reader( $obj ); $self->{capture} = []; # Prime the reader $self->_next; my $doc = $self->_read; # The terminator is mandatory otherwise we'd consume a line from the # iterator that doesn't belong to us. If we want to remove this # restriction we'll have to implement look-ahead in the iterators. # Which might not be a bad idea. my $dots = $self->_peek; croak "Missing '...' at end of YAML" unless $dots =~ $IS_END_YAML; delete $self->{reader}; delete $self->{next}; return $doc; } sub get_raw { my $self = shift; if ( defined( my $capture = $self->{capture} ) ) { return join( "\n", @$capture ) . "\n"; } return ''; } sub _peek { my $self = shift; return $self->{next} unless wantarray; my $line = $self->{next}; $line =~ /^ (\s*) (.*) $ /x; return ( $2, length $1 ); } sub _next { my $self = shift; croak "_next called with no reader" unless $self->{reader}; my $line = $self->{reader}->(); $self->{next} = $line; push @{ $self->{capture} }, $line; } sub _read { my $self = shift; my $line = $self->_peek; # Do we have a document header? if ( $line =~ /^ --- (?: \s* (.+?) \s* )? $/x ) { $self->_next; return $self->_read_scalar( $1 ) if defined $1; # Inline? my ( $next, $indent ) = $self->_peek; if ( $next =~ /^ - /x ) { return $self->_read_array( $indent ); } elsif ( $next =~ $IS_HASH_KEY ) { return $self->_read_hash( $next, $indent ); } elsif ( $next =~ $IS_END_YAML ) { croak "Premature end of YAML"; } else { croak "Unsupported YAML syntax: '$next'"; } } else { croak "YAML document header not found"; } } # Parse a double quoted string sub _read_qq { my $self = shift; my $str = shift; unless ( $str =~ s/^ " (.*?) " $/$1/x ) { die "Internal: not a quoted string"; } $str =~ s/\\"/"/gx; $str =~ s/ \\ ( [tartan\\favez] | x([0-9a-fA-F]{2}) ) / (length($1) > 1) ? pack("H2", $2) : $UNESCAPES{$1} /gex; return $str; } # Parse a scalar string to the actual scalar sub _read_scalar { my $self = shift; my $string = shift; return undef if $string eq '~'; if ( $string eq '>' || $string eq '|' ) { my ( $line, $indent ) = $self->_peek; die "Multi-line scalar content missing" unless defined $line; my @multiline = ( $line ); while ( 1 ) { $self->_next; my ( $next, $ind ) = $self->_peek; last if $ind < $indent; push @multiline, $next; } return join( ( $string eq '>' ? ' ' : "\n" ), @multiline ) . "\n"; } if ( $string =~ /^ ' (.*) ' $/x ) { ( my $rv = $1 ) =~ s/''/'/g; return $rv; } if ( $string =~ $IS_QQ_STRING ) { return $self->_read_qq( $string ); } if ( $string =~ /^['"]/ ) { # A quote with folding... we don't support that die __PACKAGE__ . " does not support multi-line quoted scalars"; } # Regular unquoted string return $string; } sub _read_nested { my $self = shift; my ( $line, $indent ) = $self->_peek; if ( $line =~ /^ -/x ) { return $self->_read_array( $indent ); } elsif ( $line =~ $IS_HASH_KEY ) { return $self->_read_hash( $line, $indent ); } else { croak "Unsupported YAML syntax: '$line'"; } } # Parse an array sub _read_array { my ( $self, $limit ) = @_; my $ar = []; while ( 1 ) { my ( $line, $indent ) = $self->_peek; last if $indent < $limit || !defined $line || $line =~ $IS_END_YAML; if ( $indent > $limit ) { croak "Array line over-indented"; } if ( $line =~ /^ (- \s+) \S+ \s* : (?: \s+ | $ ) /x ) { $indent += length $1; $line =~ s/-\s+//; push @$ar, $self->_read_hash( $line, $indent ); } elsif ( $line =~ /^ - \s* (.+?) \s* $/x ) { croak "Unexpected start of YAML" if $line =~ /^---/; $self->_next; push @$ar, $self->_read_scalar( $1 ); } elsif ( $line =~ /^ - \s* $/x ) { $self->_next; push @$ar, $self->_read_nested; } elsif ( $line =~ $IS_HASH_KEY ) { $self->_next; push @$ar, $self->_read_hash( $line, $indent, ); } else { croak "Unsupported YAML syntax: '$line'"; } } return $ar; } sub _read_hash { my ( $self, $line, $limit ) = @_; my $indent; my $hash = {}; while ( 1 ) { croak "Badly formed hash line: '$line'" unless $line =~ $HASH_LINE; my ( $key, $value ) = ( $self->_read_scalar( $1 ), $2 ); $self->_next; if ( defined $value ) { $hash->{$key} = $self->_read_scalar( $value ); } else { $hash->{$key} = $self->_read_nested; } ( $line, $indent ) = $self->_peek; last if $indent < $limit || !defined $line || $line =~ $IS_END_YAML; } return $hash; } 1; __END__ =head1 NAME Data::YAML::Reader - Parse YAML created by Data::YAML::Writer =head1 VERSION This document describes Data::YAML::Reader version 0.0.6 =head1 SYNOPSIS use Data::YAML::Reader; my $yr = Data::YAML::Reader->new; # Read from an array... my $from_array = $yr->read( \@some_array ); # ...an open file handle... my $from_handle = $yr->read( $some_file ); # ...a string containing YAML... my $from_string = $yr->read( $some_string ); # ...or a closure my $from_code = $yr->read( sub { return get_next_line() } ); =head1 DESCRIPTION In the spirit of L this is a lightweight, dependency-free YAML reader. While C is designed principally for working with configuration files C concentrates on the transparent round-tripping of YAML serialized Perl data structures. The syntax accepted by C is a subset of YAML. Specifically it is the same subset of YAML that L produces. See L for more information. =head1 INTERFACE =over =item C<< new >> Creates and returns an empty C object. No options may be passed. =item C<< read( $source ) >> Read YAML and return the data structure it represents. The YAML data may be supplied by a =over =item * scalar string containing YAML source =item * the handle of an open file =item * a reference to an array of lines =item * a code reference =back In the case of a code reference a subroutine (most likely a closure) that returns successive lines of YAML must be supplied. Lines should have no trailing newline. When the YAML is exhausted the subroutine must return undef. Returns the data structure (specifically either a scalar, hash ref or array ref) that results from decoding the YAML. =item C<< get_raw >> Return the raw YAML source from the most recent C. =back =head1 BUGS AND LIMITATIONS No bugs have been reported. Please report any bugs or feature requests to C, or through the web interface at L. =head1 SEE ALSO L, L, L, L, L =head1 AUTHOR Andy Armstrong C<< >> Adam Kennedy wrote L which provided the template and many of the YAML matching regular expressions for this module. =head1 LICENCE AND COPYRIGHT Copyright (c) 2007, Andy Armstrong C<< >>. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L. =head1 DISCLAIMER OF WARRANTY BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Data-YAML-0.0.6/lib/Data/YAML/Writer.pm0000644000076500001200000001422111154603403015716 0ustar andyadminpackage Data::YAML::Writer; use strict; use warnings; use Carp; use vars qw{$VERSION}; $VERSION = '0.0.6'; my $ESCAPE_CHAR = qr{ [\x00-\x1f\"] }x; my @UNPRINTABLE = qw( z x01 x02 x03 x04 x05 x06 a x08 t n v f r x0e x0f x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x1a e x1c x1d x1e x1f ); # Create an empty Data::YAML::Writer object sub new { my $class = shift; bless {}, $class; } sub write { my $self = shift; croak "Need something to write" unless @_; my $obj = shift; my $out = shift || \*STDOUT; croak "Need a reference to something I can write to" unless ref $out; $self->{writer} = $self->_make_writer( $out ); $self->_write_obj( '---', $obj ); $self->_put( '...' ); delete $self->{writer}; } sub _make_writer { my $self = shift; my $out = shift; my $ref = ref $out; if ( 'CODE' eq $ref ) { return $out; } elsif ( 'ARRAY' eq $ref ) { return sub { push @$out, shift }; } elsif ( 'SCALAR' eq $ref ) { return sub { $$out .= shift() . "\n" }; } elsif ( 'GLOB' eq $ref || 'IO::Handle' eq $ref ) { return sub { print $out shift(), "\n" }; } croak "Can't write to $out"; } sub _put { my $self = shift; $self->{writer}->( join '', @_ ); } sub _enc_scalar { my $self = shift; my $val = shift; return '~' unless defined $val; if ( $val =~ /$ESCAPE_CHAR/ ) { $val =~ s/\\/\\\\/g; $val =~ s/"/\\"/g; $val =~ s/ ( [\x00-\x1f] ) / '\\' . $UNPRINTABLE[ ord($1) ] /gex; return qq{"$val"}; } if ( length( $val ) == 0 or $val =~ /\s/ ) { $val =~ s/'/''/; return "'$val'"; } return $val; } sub _write_obj { my $self = shift; my $prefix = shift; my $obj = shift; my $indent = shift || 0; if ( my $ref = ref $obj ) { my $pad = ' ' x $indent; $self->_put( $prefix ); if ( 'HASH' eq $ref ) { for my $key ( sort keys %$obj ) { my $value = $obj->{$key}; $self->_write_obj( $pad . $self->_enc_scalar( $key ) . ':', $value, $indent + 1 ); } } elsif ( 'ARRAY' eq $ref ) { for my $value ( @$obj ) { $self->_write_obj( $pad . '-', $value, $indent + 1 ); } } else { croak "Don't know how to encode $ref"; } } else { $self->_put( $prefix, ' ', $self->_enc_scalar( $obj ) ); } } 1; __END__ =head1 NAME Data::YAML::Writer - Easy YAML serialisation =head1 VERSION This document describes Data::YAML::Writer version 0.0.6 =head1 SYNOPSIS use Data::YAML::Writer; my $data = { one => 1, two => 2, three => [ 1, 2, 3 ], }; my $yw = Data::YAML::Writer->new; # Write to an array... $yw->write( $data, \@some_array ); # ...an open file handle... $yw->write( $data, $some_file_handle ); # ...a string ... $yw->write( $data, \$some_string ); # ...or a closure $yw->write( $data, sub { my $line = shift; print "$line\n"; } ); =head1 DESCRIPTION Encodes a scalar, hash reference or array reference as YAML. In the spirit of L this is a lightweight, dependency-free YAML writer. While C is designed principally for working with configuration files C concentrates on the transparent round-tripping of YAML serialized Perl data structures. The syntax produced by C is a subset of YAML. Specifically it is the same subset of YAML that L consumes. See L for more information. =head1 INTERFACE =over =item C<< new >> The constructor C creates and returns an empty C object. =item C<< write( $obj, $output ) >> Encode a scalar, hash reference or array reference as YAML. my $writer = sub { my $line = shift; print SOMEFILE "$line\n"; }; my $data = { one => 1, two => 2, three => [ 1, 2, 3 ], }; my $yw = Data::YAML::Writer->new; $yw->write( $data, $writer ); The C< $output > argument may be =over =item * a reference to a scalar to append YAML to =item * the handle of an open file =item * a reference to an array into which YAML will be pushed =item * a code reference =back If you supply a code reference the subroutine will be called once for each line of output with the line as its only argument. Passed lines will have no trailing newline. =back =head1 BUGS AND LIMITATIONS No bugs have been reported. Please report any bugs or feature requests to C, or through the web interface at L. =head1 SEE ALSO L, L, L, L, L =head1 AUTHOR Andy Armstrong C<< >> =head1 LICENCE AND COPYRIGHT Copyright (c) 2007, Andy Armstrong C<< >>. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L. =head1 DISCLAIMER OF WARRANTY BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Data-YAML-0.0.6/lib/Data/YAML.pm0000644000076500001200000001032211154603403014440 0ustar andyadminpackage Data::YAML; use strict; use warnings; use vars qw{$VERSION}; $VERSION = '0.0.6'; 1; __END__ =head1 NAME Data::YAML - Easy YAML serialisation of Perl data structures =head1 VERSION This document describes Data::YAML version 0.0.6 =head1 DESCRIPTION In the spirit of L, L and L provide lightweight, dependency-free YAML handling. While C is designed principally for working with configuration files C concentrates on the transparent round- tripping of YAML serialized Perl data structures. As an example of why this distinction matters consider that C doesn't handle hashes with keys containing non-printable characters. This is fine for configuration files but likely to cause problems when handling arbitrary Perl data structures. C handles exotic hash keys correctly. The syntax accepted by C is a subset of YAML. Specifically it is the same subset of YAML that L produces. See L for more information. =head2 YAML syntax Although YAML appears to be a simple language the entire YAML specification is huge. C implements a small subset of the complete syntax trading completeness for compactness and simplicity. This restricted syntax is known (to me at least) as 'YAMLish'. These examples demonstrates the full range of supported syntax. All YAML documents must begin with '---' and end with a line containing '...'. --- Simple scalar ... Unprintable characters are represented using standard escapes in double quoted strings. --- "\t\x01\x02\n" ... Array and hashes are represented thusly --- - "This" - "is" - "an" - "array" ... --- This: is a: hash ... Structures may nest arbitrarily --- - name: 'Hash one' value: 1 - name: 'Hash two' value: 2 ... Undef is a tilde --- ~ ... =head2 Uses Use C may be used any time you need to freeze and thaw Perl data structures into a human readable format. The output from C should be readable by any YAML parser. C was originally written to allow machine-readable diagnostic information to be passed from test scripts to L. That means that if you're writing a testing system that needs to output TAP version 13 or later syntax you might find C useful. Read more about TAP and YAMLish here: L =head1 BUGS AND LIMITATIONS No bugs have been reported. Please report any bugs or feature requests to C, or through the web interface at L. =head1 AUTHOR Andy Armstrong C<< >> =head1 LICENCE AND COPYRIGHT Copyright (c) 2007, Andy Armstrong C<< >>. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L. =head1 DISCLAIMER OF WARRANTY BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Data-YAML-0.0.6/t/0000755000076500001200000000000011154603756012201 5ustar andyadminData-YAML-0.0.6/t/00.load.t0000644000076500001200000000024611154603260013513 0ustar andyadminuse Test::More tests => 2; BEGIN { use_ok( 'Data::YAML::Reader' ); use_ok( 'Data::YAML::Writer' ); } diag( "Testing Data::YAML $Data::YAML::Reader::VERSION" ); Data-YAML-0.0.6/t/10-input.t0000644000076500001200000000430511154603262013735 0ustar andyadminuse strict; use warnings; use Test::More tests => 12; use Data::Dumper; use Data::YAML::Reader; my $in = [ "---", "bill-to:", " address:", " city: \"Royal Oak\"", " lines: \"458 Walkman Dr.\\nSuite #292\\n\"", " postal: 48046", " state: MI", " family: Dumars", " given: Chris", "comments: \"Late afternoon is best. Backup contact is Nancy Billsmer \@ 338-4338\\n\"", "date: 2001-01-23", "invoice: 34843", "product:", " -", " description: Basketball", " price: 450.00", " quantity: 4", " sku: BL394D", " -", " description: \"Super Hoop\"", " price: 2392.00", " quantity: 1", " sku: BL4438H", "tax: 251.42", "total: 4443.52", "...", ]; my $out = { 'bill-to' => { 'given' => 'Chris', 'address' => { 'city' => 'Royal Oak', 'postal' => '48046', 'lines' => "458 Walkman Dr.\nSuite #292\n", 'state' => 'MI' }, 'family' => 'Dumars' }, 'invoice' => '34843', 'date' => '2001-01-23', 'tax' => '251.42', 'product' => [ { 'sku' => 'BL394D', 'quantity' => '4', 'price' => '450.00', 'description' => 'Basketball' }, { 'sku' => 'BL4438H', 'quantity' => '1', 'price' => '2392.00', 'description' => 'Super Hoop' } ], 'comments' => "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n", 'total' => '4443.52' }; my @lines = @$in; my $scalar = join( "\n", @lines ) . "\n"; my @source = ( { name => 'Array reference', source => $in, }, { name => 'Closure', source => sub { shift @lines }, }, { name => 'Scalar', source => $scalar, }, { name => 'Scalar ref', source => \$scalar, }, ); for my $src ( @source ) { my $name = $src->{name}; ok my $yaml = Data::YAML::Reader->new, "$name: Created"; isa_ok $yaml, 'Data::YAML::Reader'; my $got = eval { $yaml->read( $src->{source} ) }; unless ( is_deeply $got, $out, "$name: Result matches" ) { local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1; diag( Data::Dumper->Dump( [$got], ['$got'] ) ); diag( Data::Dumper->Dump( [$out], ['$expected'] ) ); } } Data-YAML-0.0.6/t/20-reader.t0000644000076500001200000002551211154603265014047 0ustar andyadminuse strict; use warnings; use Test::More; use Data::Dumper; use Data::YAML::Reader; my @SCHEDULE; BEGIN { @SCHEDULE = ( { name => 'Hello World', in => [ '--- Hello, World', '...', ], out => "Hello, World", }, { name => 'Hello World 2', in => [ '--- \'Hello, \'\'World\'', '...', ], out => "Hello, 'World", }, { name => 'Hello World 3', in => [ '--- "Hello, World"', '...', ], out => "Hello, World", }, { name => 'Hello World 4', in => [ '--- "Hello, World"', '...', ], out => "Hello, World", }, { name => 'Hello World 4', in => [ '--- >', ' Hello,', ' World', '...', ], out => "Hello, World\n", }, { name => 'Hello World 5', in => [ '--- >', ' Hello,', ' World', '...', ], error => qr{Missing\s+'[.][.][.]'}, }, { name => 'Simple array', in => [ '---', '- 1', '- 2', '- 3', '...', ], out => [ '1', '2', '3' ], }, { name => 'Mixed array', in => [ '---', '- 1', '- \'two\'', '- "three\n"', '...', ], out => [ '1', 'two', "three\n" ], }, { name => 'Hash in array', in => [ '---', '- 1', '- two: 2', '- 3', '...', ], out => [ '1', { two => '2' }, '3' ], }, { name => 'Hash in array 2', in => [ '---', '- 1', '- two: 2', ' three: 3', '- 4', '...', ], out => [ '1', { two => '2', three => '3' }, '4' ], }, { name => 'Nested array', in => [ '---', '- one', '-', ' - two', ' -', ' - three', ' - four', '- five', '...', ], out => [ 'one', [ 'two', ['three'], 'four' ], 'five' ], }, { name => 'Nested hash', in => [ '---', 'one:', ' five: 5', ' two:', ' four: 4', ' three: 3', 'six: 6', '...', ], out => { one => { two => { three => '3', four => '4' }, five => '5' }, six => '6' }, }, { name => 'Original YAML::Tiny test', in => [ '---', 'invoice: 34843', 'date : 2001-01-23', 'bill-to:', ' given : Chris', ' family : Dumars', ' address:', ' lines: |', ' 458 Walkman Dr.', ' Suite #292', ' city : Royal Oak', ' state : MI', ' postal : 48046', 'product:', ' - sku : BL394D', ' quantity : 4', ' description : Basketball', ' price : 450.00', ' - sku : BL4438H', ' quantity : 1', ' description : Super Hoop', ' price : 2392.00', 'tax : 251.42', 'total: 4443.52', 'comments: >', ' Late afternoon is best.', ' Backup contact is Nancy', ' Billsmer @ 338-4338', '...', ], out => { 'bill-to' => { 'given' => 'Chris', 'address' => { 'city' => 'Royal Oak', 'postal' => '48046', 'lines' => "458 Walkman Dr.\nSuite #292\n", 'state' => 'MI' }, 'family' => 'Dumars' }, 'invoice' => '34843', 'date' => '2001-01-23', 'tax' => '251.42', 'product' => [ { 'sku' => 'BL394D', 'quantity' => '4', 'price' => '450.00', 'description' => 'Basketball' }, { 'sku' => 'BL4438H', 'quantity' => '1', 'price' => '2392.00', 'description' => 'Super Hoop' } ], 'comments' => "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n", 'total' => '4443.52' } }, # Tests harvested from YAML::Tiny { in => ['...'], name => 'Regression: empty', error => qr{document\s+header\s+not\s+found} }, { in => [ '# comment', '...' ], name => 'Regression: only_comment', error => qr{document\s+header\s+not\s+found} }, { out => undef, in => [ '---', '...' ], name => 'Regression: only_header', error => qr{Premature\s+end}i, }, { out => undef, in => [ '---', '---', '...' ], name => 'Regression: two_header', error => qr{Unexpected\s+start}i, }, { out => undef, in => [ '--- ~', '...' ], name => 'Regression: one_undef' }, { out => undef, in => [ '--- ~', '...' ], name => 'Regression: one_undef2' }, { in => [ '--- ~', '---', '...' ], name => 'Regression: two_undef', error => qr{Missing\s+'[.][.][.]'}, }, { out => 'foo', in => [ '--- foo', '...' ], name => 'Regression: one_scalar', }, { out => 'foo', in => [ '--- foo', '...' ], name => 'Regression: one_scalar2', }, { in => [ '--- foo', '--- bar', '...' ], name => 'Regression: two_scalar', error => qr{Missing\s+'[.][.][.]'}, }, { out => ['foo'], in => [ '---', '- foo', '...' ], name => 'Regression: one_list1' }, { out => [ 'foo', 'bar' ], in => [ '---', '- foo', '- bar', '...' ], name => 'Regression: one_list2' }, { out => [ undef, 'bar' ], in => [ '---', '- ~', '- bar', '...' ], name => 'Regression: one_listundef' }, { out => { 'foo' => 'bar' }, in => [ '---', 'foo: bar', '...' ], name => 'Regression: one_hash1' }, { out => { 'foo' => 'bar', 'this' => undef }, in => [ '---', 'foo: bar', 'this: ~', '...' ], name => 'Regression: one_hash2' }, { out => { 'foo' => [ 'bar', undef, 'baz' ] }, in => [ '---', 'foo:', ' - bar', ' - ~', ' - baz', '...' ], name => 'Regression: array_in_hash' }, { out => { 'bar' => { 'foo' => 'bar' }, 'foo' => undef }, in => [ '---', 'foo: ~', 'bar:', ' foo: bar', '...' ], name => 'Regression: hash_in_hash' }, { out => [ { 'foo' => undef, 'this' => 'that' }, 'foo', undef, { 'foo' => 'bar', 'this' => 'that' } ], in => [ '---', '-', ' foo: ~', ' this: that', '- foo', '- ~', '-', ' foo: bar', ' this: that', '...' ], name => 'Regression: hash_in_array' }, { out => ['foo'], in => [ '---', '- \'foo\'', '...' ], name => 'Regression: single_quote1' }, { out => [' '], in => [ '---', '- \' \'', '...' ], name => 'Regression: single_spaces' }, { out => [''], in => [ '---', '- \'\'', '...' ], name => 'Regression: single_null' }, { out => ' ', in => [ '--- " "', '...' ], name => 'Regression: only_spaces' }, { out => [ undef, { 'foo' => 'bar', 'this' => 'that' }, 'baz' ], in => [ '---', '- ~', '- foo: bar', ' this: that', '- baz', '...' ], name => 'Regression: inline_nested_hash' }, { name => "Unprintables", in => [ "---", "- \"\\z\\x01\\x02\\x03\\x04\\x05\\x06\\a\\x08\\t\\n\\v\\f\\r\\x0e\\x0f\"", "- \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\e\\x1c\\x1d\\x1e\\x1f\"", "- \" !\\\"#\$%&'()*+,-./\"", "- 0123456789:;<=>?", "- '\@ABCDEFGHIJKLMNO'", "- 'PQRSTUVWXYZ[\\]^_'", "- '`abcdefghijklmno'", "- 'pqrstuvwxyz{|}~\177'", "- \200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217", "- \220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237", "- \240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257", "- \260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277", "- \300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317", "- \320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337", "- \340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357", "- \360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377", "..." ], out => [ "\0\1\2\3\4\5\6\a\b\t\n\13\f\r\16\17", "\20\21\22\23\24\25\26\27\30\31\32\e\34\35\36\37", " !\"#\$%&'()*+,-./", "0123456789:;<=>?", "\@ABCDEFGHIJKLMNO", "PQRSTUVWXYZ[\\]^_", "`abcdefghijklmno", "pqrstuvwxyz{|}~\177", "\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217", "\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237", "\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257", "\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277", "\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317", "\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337", "\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357", "\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377" ], }, { name => 'Quoted hash keys', in => [ '---', ' "quoted": Magic!', ' "\n\t": newline, tab', '...', ], out => { quoted => 'Magic!', "\n\t" => 'newline, tab', }, }, ); plan tests => @SCHEDULE * 5; } sub iter { my $ar = shift; return sub { return shift @$ar; }; } for my $test ( @SCHEDULE ) { my $name = $test->{name}; ok my $yaml = Data::YAML::Reader->new, "$name: Created"; isa_ok $yaml, 'Data::YAML::Reader'; # diag "$name\n"; # unless ( $test->{in} ) { # pass for 1 .. 2; # use YAML; # diag "Input for test:\n"; # diag( Dump( $test->{out} ) ); # next; # } my $source = join( "\n", @{ $test->{in} } ) . "\n"; my $iter = iter( $test->{in} ); my $got = eval { $yaml->read( $iter ) }; my $raw = $yaml->get_raw; if ( my $err = $test->{error} ) { unless ( like $@, $err, "$name: Error message" ) { diag "Error: $@\n"; } ok !$got, "$name: No result"; pass; } else { my $want = $test->{out}; unless ( ok !$@, "$name: No error" ) { diag "Error: $@\n"; } unless ( is_deeply $got, $want, "$name: Result matches" ) { local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1; diag( Data::Dumper->Dump( [$got], ['$got'] ) ); diag( Data::Dumper->Dump( [$want], ['$expected'] ) ); } is $raw, $source, "$name: Captured source matches"; } } Data-YAML-0.0.6/t/30-output.t0000644000076500001200000000455411154603270014145 0ustar andyadminuse strict; use warnings; use Test::More tests => 9; use Data::Dumper; use Data::YAML::Writer; my $out = [ "---", "bill-to:", " address:", " city: 'Royal Oak'", " lines: \"458 Walkman Dr.\\nSuite #292\\n\"", " postal: 48046", " state: MI", " family: Dumars", " given: Chris", "comments: \"Late afternoon is best. Backup contact is Nancy Billsmer \@ 338-4338\\n\"", "date: 2001-01-23", "invoice: 34843", "product:", " -", " description: Basketball", " price: 450.00", " quantity: 4", " sku: BL394D", " -", " description: 'Super Hoop'", " price: 2392.00", " quantity: 1", " sku: BL4438H", "tax: 251.42", "total: 4443.52", "...", ]; my $in = { 'bill-to' => { 'given' => 'Chris', 'address' => { 'city' => 'Royal Oak', 'postal' => '48046', 'lines' => "458 Walkman Dr.\nSuite #292\n", 'state' => 'MI' }, 'family' => 'Dumars' }, 'invoice' => '34843', 'date' => '2001-01-23', 'tax' => '251.42', 'product' => [ { 'sku' => 'BL394D', 'quantity' => '4', 'price' => '450.00', 'description' => 'Basketball' }, { 'sku' => 'BL4438H', 'quantity' => '1', 'price' => '2392.00', 'description' => 'Super Hoop' } ], 'comments' => "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n", 'total' => '4443.52' }; my @buf1 = (); my @buf2 = (); my $buf3 = ''; my @destination = ( { name => 'Array reference', destination => \@buf1, normalise => sub { return \@buf1 }, }, { name => 'Closure', destination => sub { push @buf2, shift }, normalise => sub { return \@buf2 }, }, { name => 'Scalar', destination => \$buf3, normalise => sub { my @ar = split( /\n/, $buf3 ); return \@ar; }, }, ); for my $dest ( @destination ) { my $name = $dest->{name}; ok my $yaml = Data::YAML::Writer->new, "$name: Created"; isa_ok $yaml, 'Data::YAML::Writer'; $yaml->write( $in, $dest->{destination} ); my $got = $dest->{normalise}->(); unless ( is_deeply $got, $out, "$name: Result matches" ) { local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1; diag( Data::Dumper->Dump( [$got], ['$got'] ) ); diag( Data::Dumper->Dump( [$out], ['$expected'] ) ); } } Data-YAML-0.0.6/t/40-writer.t0000644000076500001200000001140211154603273014113 0ustar andyadmin#!/usr/bin/perl use strict; use warnings; use Test::More; use Data::Dumper; use Data::YAML::Reader; use Data::YAML::Writer; my @SCHEDULE; BEGIN { @SCHEDULE = ( { name => 'Simple scalar', in => 1, out => [ '--- 1', '...', ], }, { name => 'Undef', in => undef, out => [ '--- ~', '...', ], }, { name => 'Unprintable', in => "\x01\n\t", out => [ '--- "\x01\n\t"', '...', ], }, { name => 'Simple array', in => [ 1, 2, 3 ], out => [ '---', '- 1', '- 2', '- 3', '...', ], }, { name => 'Array, two elements, undef', in => [ undef, undef ], out => [ '---', '- ~', '- ~', '...', ], }, { name => 'Nested array', in => [ 1, 2, [ 3, 4 ], 5 ], out => [ '---', '- 1', '- 2', '-', ' - 3', ' - 4', '- 5', '...', ], }, { name => 'Simple hash', in => { one => '1', two => '2', three => '3' }, out => [ '---', 'one: 1', 'three: 3', 'two: 2', '...', ], }, { name => 'Nested hash', in => { one => '1', two => '2', more => { three => '3', four => '4' } }, out => [ '---', 'more:', ' four: 4', ' three: 3', 'one: 1', 'two: 2', '...', ], }, { name => 'Unprintable key', in => { one => '1', "\x02" => '2', three => '3' }, out => [ '---', '"\x02": 2', 'one: 1', 'three: 3', '...', ], }, { name => 'Empty key', in => { '' => 'empty' }, out => [ '---', "'': empty", '...', ], }, { name => 'Empty value', in => { '' => '' }, out => [ '---', "'': ''", '...', ], }, { name => 'Complex', in => { 'bill-to' => { 'given' => 'Chris', 'address' => { 'city' => 'Royal Oak', 'postal' => '48046', 'lines' => "458 Walkman Dr.\nSuite #292\n", 'state' => 'MI' }, 'family' => 'Dumars' }, 'invoice' => '34843', 'date' => '2001-01-23', 'tax' => '251.42', 'product' => [ { 'sku' => 'BL394D', 'quantity' => '4', 'price' => '450.00', 'description' => 'Basketball' }, { 'sku' => 'BL4438H', 'quantity' => '1', 'price' => '2392.00', 'description' => 'Super Hoop' } ], 'comments' => "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n", 'total' => '4443.52' }, out => [ "---", "bill-to:", " address:", " city: 'Royal Oak'", " lines: \"458 Walkman Dr.\\nSuite #292\\n\"", " postal: 48046", " state: MI", " family: Dumars", " given: Chris", "comments: \"Late afternoon is best. Backup contact is Nancy Billsmer \@ 338-4338\\n\"", "date: 2001-01-23", "invoice: 34843", "product:", " -", " description: Basketball", " price: 450.00", " quantity: 4", " sku: BL394D", " -", " description: 'Super Hoop'", " price: 2392.00", " quantity: 1", " sku: BL4438H", "tax: 251.42", "total: 4443.52", "...", ], }, ); plan tests => @SCHEDULE * 5; } sub iter { my $ar = shift; return sub { return shift @$ar; }; } for my $test ( @SCHEDULE ) { my $name = $test->{name}; ok my $yaml = Data::YAML::Writer->new, "$name: Created"; isa_ok $yaml, 'Data::YAML::Writer'; my $got = []; my $writer = sub { push @$got, shift }; my $data = $test->{in}; eval { $yaml->write( $data, $writer ) }; if ( my $err = $test->{error} ) { unless ( like $@, $err, "$name: Error message" ) { diag "Error: $@\n"; } is_deeply $got, [], "$name: No result"; pass; } else { my $want = $test->{out}; unless ( ok !$@, "$name: No error" ) { diag "Error: $@\n"; } unless ( is_deeply $got, $want, "$name: Result matches" ) { local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1; diag( Data::Dumper->Dump( [$got], ['$got'] ) ); diag( Data::Dumper->Dump( [$want], ['$expected'] ) ); } my $yr = Data::YAML::Reader->new; # Now try parsing it my $reader = sub { shift @$got }; my $parsed = $yr->read( $reader ); unless ( is_deeply $parsed, $data, "$name: Reparse OK" ) { local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1; diag( Data::Dumper->Dump( [$parsed], ['$parsed'] ) ); diag( Data::Dumper->Dump( [$data], ['$data'] ) ); } } } Data-YAML-0.0.6/t/pod-coverage.t0000644000076500001200000000036211154603276014737 0ustar andyadmin#!perl -T use Test::More; eval "use Test::Pod::Coverage 1.04"; plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; all_pod_coverage_ok( { private => [ qr{^BUILD|DEMOLISH|AUTOMETHOD|START$}, qr{^_} ] } ); Data-YAML-0.0.6/t/pod.t0000644000076500001200000000021411154603300013126 0ustar andyadmin#!perl -T use Test::More; eval "use Test::Pod 1.14"; plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; all_pod_files_ok();