Subject: | Data::YAML 0.0.6 does not handle [] and {} |
Date: | Fri, 12 Jun 2009 10:18:53 +0200 |
To: | bug-data-yaml [...] rt.cpan.org |
From: | Christian Augustin <cma [...] snafu.de> |
Hi,
Data::YAML 0.0.6 does not handle empty ARRAYs and HASHes. If those are
part of the data structure given to Data::YAML::Writer,
Data::YAML::Reader croaks.
It could be handled the way YAML::Tiny does it. Here the diffs of a
work-around (together with the fix for the single quote problem):
Reader.pm:
9c9
< $VERSION = '0.0.6';
---
Show quoted text
> $VERSION = '0.0.6.1';
28c28,29
< my $HASH_LINE = qr{ ^ ($QQ_STRING|\S+) \s* : (?: \s+ (.+?) \s* )?
$ }x;
---
Show quoted text > my $SQ_STRING = qr{ ' (?: '' | [^'] )* ' }x;
> my $HASH_LINE = qr{ ^ ( $SQ_STRING | $QQ_STRING | \S+ ) \s* : (?: \s
+ (.+?) \s* )? $ }x;
175a177,178
Show quoted text > return [] if $string eq '[]';
> return {} if $string eq '{}';
Writer.pm:
9c9
< $VERSION = '0.0.6';
---
Show quoted text > $VERSION = '0.0.6.1';
86c86
< if ( length( $val ) == 0 or $val =~ /\s/ ) {
---
Show quoted text > if ( length( $val ) == 0 or $val =~ /[\s\{\}\[\]]/ ) {
102d101
< $self->_put( $prefix );
104,107c103,112
< for my $key ( sort keys %$obj ) {
< my $value = $obj->{$key};
< $self->_write_obj( $pad . $self->_enc_scalar( $key ) . ':',
< $value, $indent + 1 );
---
Show quoted text > if (keys %$obj) {
> $self->_put($prefix);
> for my $key ( sort keys %$obj ) {
> my $value = $obj->{$key};
> $self->_write_obj( $pad . $self->_enc_scalar($key) . ':',
> $value, $indent + 1 );
> }
> }
> else {
> $self->_put( $prefix, ' {}' );
111,112c116,123
< for my $value ( @$obj ) {
< $self->_write_obj( $pad . '-', $value, $indent + 1 );
---
Show quoted text > if (scalar @$obj) {
> $self->_put($prefix);
> for my $value (@$obj) {
> $self->_write_obj( $pad . '-', $value, $indent + 1 );
> }
> }
> else {
> $self->_put( $prefix, ' []' );
The Writer.pm solution isn't elegant, but it does the job. The changes
to the Reader.pm are short and rather straight-forward.
Kind regards
--
Christian Augustin
mail@caugustin.de
Skype caugustin.de
Mobile +49-171-6222361, Fax +49-30-2639173-01906
Holsteinische Str. 19, 12161 Berlin, GERMANY