Skip Menu |

This queue is for tickets about the YAML-Tiny CPAN distribution.

Report information
The Basics
Id: 33922
Status: resolved
Priority: 0/
Queue: YAML-Tiny

People
Owner: Nobody in particular
Requestors: ben [...] morrow.me.uk
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Load and LoadFile unhelpful in scalar context
Date: Sun, 9 Mar 2008 11:51:11 +0000
To: bug-YAML-Tiny [...] rt.cpan.org
From: Ben Morrow <ben [...] morrow.me.uk>
Load and LoadFile both return the number of documents in the stream when called in scalar context. In the common case of reading a simple configuration file with one document in it, this is not very helpful. The patch below is a simple fix. Ben diff -ur YAML-Tiny~/lib/YAML/Tiny.pm YAML-Tiny/lib/YAML/Tiny.pm --- YAML-Tiny~/lib/YAML/Tiny.pm Sun Mar 9 11:39:52 2008 +++ YAML-Tiny/lib/YAML/Tiny.pm Sun Mar 9 11:41:30 2008 @@ -470,7 +470,7 @@ sub Load { my $self = YAML::Tiny->read_string(@_) or Carp::croak("Failed to load YAML document from string"); - return @$self; + return wantarray ? @$self : $self->[0]; } BEGIN { @@ -486,7 +486,7 @@ sub LoadFile { my $self = YAML::Tiny->read($_[0]) or Carp::croak("Failed to load YAML document from '" . ($_[0] || '') . "'"); - return @$self; + return wantarray ? @$self : $self->[0]; }
Subject: Re: [rt.cpan.org #33922] Load and LoadFile unhelpful in scalar context
Date: Mon, 10 Mar 2008 10:41:46 +1100
To: bug-YAML-Tiny [...] rt.cpan.org
From: Adam Kennedy <adamkennedybackup [...] gmail.com>
The behavior I intend to follow is exactly the same behavior as YAML.pm itself. That said, I'm not sure I'm doing THAT correctly atm either. I'll have a look into what YAML.pm does, and correct either myself, or it, accordingly. Adam K ben@morrow.me.uk via RT wrote: Show quoted text
> Sun Mar 09 07:51:43 2008: Request 33922 was acted upon. > Transaction: Ticket created by ben@morrow.me.uk > Queue: YAML-Tiny > Subject: Load and LoadFile unhelpful in scalar context > Broken in: (no value) > Severity: (no value) > Owner: Nobody > Requestors: ben@morrow.me.uk > Status: new > Ticket <URL: http://rt.cpan.org/Ticket/Display.html?id=33922 > > > > Load and LoadFile both return the number of documents in the stream when > called in scalar context. In the common case of reading a simple > configuration file with one document in it, this is not very helpful. > The patch below is a simple fix. > > Ben > > diff -ur YAML-Tiny~/lib/YAML/Tiny.pm YAML-Tiny/lib/YAML/Tiny.pm > --- YAML-Tiny~/lib/YAML/Tiny.pm Sun Mar 9 11:39:52 2008 > +++ YAML-Tiny/lib/YAML/Tiny.pm Sun Mar 9 11:41:30 2008 > @@ -470,7 +470,7 @@ > sub Load { > my $self = YAML::Tiny->read_string(@_) > or Carp::croak("Failed to load YAML document from string"); > - return @$self; > + return wantarray ? @$self : $self->[0]; > } > > BEGIN { > @@ -486,7 +486,7 @@ > sub LoadFile { > my $self = YAML::Tiny->read($_[0]) > or Carp::croak("Failed to load YAML document from '" . ($_[0] || '') . "'"); > - return @$self; > + return wantarray ? @$self : $self->[0]; > } > > > >
YAML::Tiny now behaves identically to YAML.pm in scalar context.
Subject: Re: [rt.cpan.org #33922] Load and LoadFile unhelpful in scalar context
Date: Thu, 20 Mar 2008 17:15:23 +0000
To: Adam Kennedy via RT <bug-YAML-Tiny [...] rt.cpan.org>
From: Ben Morrow <ben [...] morrow.me.uk>
At 6AM on 20/03/08 you (Adam Kennedy via RT) wrote: Show quoted text
> > YAML::Tiny now behaves identically to YAML.pm in scalar context.
Thank you for fixing this so quickly; unfortunately, you missed LoadFile. Patch attached. Ben
diff -Nur YAML-Tiny-1.26/MANIFEST YAML-Tiny/MANIFEST --- YAML-Tiny-1.26/MANIFEST Thu Mar 20 10:20:06 2008 +++ YAML-Tiny/MANIFEST Thu Mar 20 17:01:15 2008 @@ -3,6 +3,7 @@ LICENSE Makefile.PL MANIFEST This list of files +META.yml Module meta-data (added by MakeMaker) README t/01_compile.t t/02_basic.t @@ -20,9 +21,10 @@ t/99_pmv.t t/data/HTML-WebDAO.yml t/data/multibyte.yml +t/data/one.yml t/data/sample.yml t/data/Template-Provider-Unicode-Japanese.yml t/data/toolbar.yml +t/data/two.yml t/data/vanilla.yml t/lib/Test.pm -META.yml Module meta-data (added by MakeMaker) diff -Nur YAML-Tiny-1.26/lib/YAML/Tiny.pm YAML-Tiny/lib/YAML/Tiny.pm --- YAML-Tiny-1.26/lib/YAML/Tiny.pm Thu Mar 20 10:19:47 2008 +++ YAML-Tiny/lib/YAML/Tiny.pm Thu Mar 20 17:02:50 2008 @@ -491,7 +491,12 @@ sub LoadFile { my $self = YAML::Tiny->read($_[0]) or Carp::croak("Failed to load YAML document from '" . ($_[0] || '') . "'"); - return @$self; + if (wantarray) { + return @$self; + } else { + # To match YAML.pm, return the LAST document + return $self->[-1]; + } } diff -Nur YAML-Tiny-1.26/t/04_scalar.t YAML-Tiny/t/04_scalar.t --- YAML-Tiny-1.26/t/04_scalar.t Thu Mar 20 10:19:47 2008 +++ YAML-Tiny/t/04_scalar.t Thu Mar 20 16:56:15 2008 @@ -11,7 +11,7 @@ use t::lib::Test; use Test::More; if ( t::lib::Test->have_yamlpm ) { - plan( tests => 8 ); + plan( tests => 12 ); } else { plan( skip_all => 'Requites YAML.pm' ); exit(0); @@ -19,6 +19,7 @@ use YAML (); use YAML::Tiny (); +use File::Spec::Functions qw/catfile/; @@ -39,6 +40,9 @@ - bar END_YAML +my $one_file = catfile( 't', 'data', 'one.yml' ); +my $two_file = catfile( 't', 'data', 'two.yml' ); + @@ -74,3 +78,22 @@ is_deeply( $two_scalar_pm, [ 'bar' ], 'two: Parsed correctly' ); is_deeply( $two_scalar_pm, $two_scalar_tiny, 'two: Scalar context matches' ); + + + + + +###################################################################### +# Match LoadFile Behaviour + +my $one_file_pm = YAML::LoadFile( $one_file ); +my $two_file_pm = YAML::LoadFile( $two_file ); +my $one_file_tiny = YAML::Tiny::LoadFile( $one_file ); +my $two_file_tiny = YAML::Tiny::LoadFile( $two_file ); + +is_deeply( $one_file_pm, [ 'foo' ], 'one: Parsed correctly' ); +is_deeply( $one_file_pm, $one_file_tiny, 'one: scalar LoadFile matches' ); + +is_deeply( $two_file_pm, [ 'bar' ], 'two: Parsed correctly' ); +is_deeply( $two_file_pm, $two_file_tiny, 'two: scalar LoadFile matches' ); + diff -Nur YAML-Tiny-1.26/t/data/one.yml YAML-Tiny/t/data/one.yml --- YAML-Tiny-1.26/t/data/one.yml Thu Jan 1 01:00:00 1970 +++ YAML-Tiny/t/data/one.yml Thu Mar 20 16:56:39 2008 @@ -0,0 +1,2 @@ +--- +- foo diff -Nur YAML-Tiny-1.26/t/data/two.yml YAML-Tiny/t/data/two.yml --- YAML-Tiny-1.26/t/data/two.yml Thu Jan 1 01:00:00 1970 +++ YAML-Tiny/t/data/two.yml Thu Mar 20 16:56:50 2008 @@ -0,0 +1,4 @@ +--- +- foo +--- +- bar
Fixed LoadFile and added explicit tests for it