From: Ricardo SIGNES Date: Thu, 16 Oct 2008 15:50:14 +0000 (+0000) Subject: flatten sclars X-Git-Tag: 0_09~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e29de5ed63ff71dba5a58a083e0555fdab510782;p=gitmo%2FMoose-Autobox.git flatten sclars --- diff --git a/lib/Moose/Autobox/Scalar.pm b/lib/Moose/Autobox/Scalar.pm index cb25538..126bf3b 100644 --- a/lib/Moose/Autobox/Scalar.pm +++ b/lib/Moose/Autobox/Scalar.pm @@ -5,7 +5,8 @@ our $VERSION = '0.01'; with 'Moose::Autobox::String', 'Moose::Autobox::Number'; - + +sub flatten { $_[0] } sub print { CORE::print $_[0] } sub say { CORE::print $_[0], "\n" } 1; @@ -33,6 +34,15 @@ as the combination (union sort of) of a String and a Number. =item B +=item B + +Flattening a scalar just returns the scalar. This means that you can say: + + my @array = $input->flatten; + + # Given $input of 5, @array is (5); + # Given $input of [ 5, 2, 0], @array is (5, 2, 0) + =back =head1 BUGS diff --git a/t/001_basic.t b/t/001_basic.t index 4c18e6f..17b81ff 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 69; +use Test::More tests => 70; BEGIN { use_ok('Moose::Autobox'); @@ -24,6 +24,9 @@ ok($s->defined, '... got a defined value'); eval $s->dump; is($VAR1, 5 , '... eval of SCALAR->dump works'); +eval $s->flatten; +is($VAR1, 5 , '... eval of SCALAR->flatten works'); + eval $s->perl; is($s->perl, $s->dump, '... SCALAR->dump equals SCALAR->perl'); diff --git a/t/008_flatten.t b/t/008_flatten.t index bc52233..888024c 100644 --- a/t/008_flatten.t +++ b/t/008_flatten.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 2; +use Test::More tests => 4; use Moose::Autobox; my $array = [ qw(1 2 3 4 ) ]; @@ -19,3 +19,17 @@ is_deeply( [ qw(1 2 a b) ], "flattening a hash returns a list", ); + +my $scalar = 1; +is_deeply( + [ $scalar->flatten ], + [ 1 ], + "flattening a scalar returns the scalar", +); + +my $scalar_ref = \$scalar; +is_deeply( + [ $scalar_ref->flatten ], + [ \$scalar ], + "flattening a reference to a scalar returns the same scalar reference", +);