From: Steffen Schwigon Date: Fri, 25 May 2007 22:14:22 +0000 (+0000) Subject: - Moose::Autobox::Scalar/Array/Hash X-Git-Tag: 0_04^0 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3f4dd8b74b711eefe941a391a327389fb05abf7f;p=gitmo%2FMoose-Autobox.git - Moose::Autobox::Scalar/Array/Hash - added &print/&say to SCALAR, ARRAY and HASH for even more Perl6 likeliness --- diff --git a/Changes b/Changes index a29bdca..3c0dacd 100644 --- a/Changes +++ b/Changes @@ -7,10 +7,17 @@ Revision history for Perl extension Moose::Autobox * Moose::Autobox::String - fixed rindex method - * Moose::Autobox::Ref + * Moose::Autobox::Item + - moved &dump from ::Ref to ::Item, so that it can be used + for the other types too + - added &perl method which is an alias for &dump for symmetry with Perl6's .perl - + + * Moose::Autobox::Scalar/Array/Hash + - added &print/&say to SCALAR, ARRAY and HASH + + 0.03 Thurs. Aug 17, 2006 * Moose::Autobox diff --git a/lib/Moose/Autobox/Array.pm b/lib/Moose/Autobox/Array.pm index 3525595..ab88261 100644 --- a/lib/Moose/Autobox/Array.pm +++ b/lib/Moose/Autobox/Array.pm @@ -139,6 +139,11 @@ sub one { return Perl6::Junction::One->one(@$array); } +## Print + +sub print { CORE::print @{$_[0]} } +sub say { CORE::print @{$_[0]}, "\n" } + 1; __END__ @@ -245,6 +250,10 @@ This is a role to describe operations on the Array type. =item B +=item B + +=item B + =back =head1 BUGS diff --git a/lib/Moose/Autobox/Hash.pm b/lib/Moose/Autobox/Hash.pm index 270f4ae..96cbe52 100644 --- a/lib/Moose/Autobox/Hash.pm +++ b/lib/Moose/Autobox/Hash.pm @@ -52,6 +52,9 @@ sub kv { [ CORE::map { [ $_, $hash->{$_} ] } CORE::keys %$hash ]; } +sub print { CORE::print %{$_[0]} } +sub say { CORE::print %{$_[0]}, "\n" } + 1; __END__ @@ -107,6 +110,10 @@ shallow merging. =item B +=item B + +=item B + =back =head1 BUGS diff --git a/lib/Moose/Autobox/Item.pm b/lib/Moose/Autobox/Item.pm index 08a6c61..af4bfd0 100644 --- a/lib/Moose/Autobox/Item.pm +++ b/lib/Moose/Autobox/Item.pm @@ -5,6 +5,14 @@ our $VERSION = '0.01'; requires 'defined'; +sub dump { + my $self = shift; + require Data::Dumper; + return Data::Dumper::Dumper($self); +} + +*perl = *dump; + 1; __END__ @@ -25,6 +33,18 @@ This is the root of our role hierarchy. =item B +=item B + +Calls Data::Dumper::Dumper. + +=item B + +Same as B. For symmetry with Perl6's .perl method. + +Like &print with newline. + +=item B + =back =head1 REQUIRED METHODS @@ -54,4 +74,4 @@ L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. -=cut \ No newline at end of file +=cut diff --git a/lib/Moose/Autobox/Ref.pm b/lib/Moose/Autobox/Ref.pm index 843d081..579f4cc 100644 --- a/lib/Moose/Autobox/Ref.pm +++ b/lib/Moose/Autobox/Ref.pm @@ -5,14 +5,6 @@ our $VERSION = '0.02'; with 'Moose::Autobox::Defined'; -sub dump { - my $self = shift; - require Data::Dumper; - return Data::Dumper::Dumper($self); -} - -*perl = *dump; - 1; __END__ @@ -33,14 +25,6 @@ This is a role to describes a reference value. =item B -=item B - -Calls Data::Dumper::Dumper. - -=item B - -Same as B. For symmetry with Perl6's .perl method. - =back =head1 BUGS diff --git a/lib/Moose/Autobox/Scalar.pm b/lib/Moose/Autobox/Scalar.pm index 7f358eb..9236510 100644 --- a/lib/Moose/Autobox/Scalar.pm +++ b/lib/Moose/Autobox/Scalar.pm @@ -6,6 +6,8 @@ our $VERSION = '0.01'; with 'Moose::Autobox::String', 'Moose::Autobox::Number'; +sub print { CORE::print $_[0] } +sub say { CORE::print $_[0], "\n" } 1; __END__ @@ -27,6 +29,10 @@ as the combination (union sort of) of a String and a Number. =item B +=item B + +=item B + =back =head1 BUGS @@ -48,4 +54,4 @@ L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. -=cut \ No newline at end of file +=cut diff --git a/t/001_basic.t b/t/001_basic.t index aeeb86e..dd6c4af 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 58; +use Test::More tests => 62; BEGIN { use_ok('Moose::Autobox'); @@ -11,6 +11,8 @@ BEGIN { use Moose::Autobox; +my $VAR1; # for eval of dumps + # SCALAR & UNDEF my $s; @@ -19,6 +21,12 @@ ok(!$s->defined, '... got a undefined value'); $s = 5; ok($s->defined, '... got a defined value'); +eval $s->dump; +is($VAR1, 5 , '... eval of SCALAR->dump works'); + +eval $s->perl; +is($s->perl, $s->dump, '... SCALAR->dump equals SCALAR->perl'); + # CODE my $f1 = sub { @_ }; @@ -174,11 +182,11 @@ $a, [ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ], '... the value is correctly put'); -my $expected_dump = qr/^\$VAR1\s*=\s*\[\s*15,\s*20,\s*30,\s*10,\s*2,\s*6,\s*78,\s*101,\s*2,\s*10,\s*15,\s*20,\s*30\s*\]\s*;\s*$/msx; +eval($a->dump); +is_deeply( $VAR1, + [ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ], + '... the value is correctly dumped'); -like( $a->dump, - $expected_dump, - '... the value is correctly dumped' ); is( $a->dump, $a->perl, '... the value is correctly dumped with perl()' ); @@ -227,4 +235,12 @@ $h->merge({ three => 33, four => 44 }), { one => 1, two => 2, three => 33, four => 44 }, '... got the hashes merged correctly'); +eval($h->dump); +is_deeply( $VAR1, + { one => 1, two => 2, three => 3 }, + '... the value is correctly dumped'); + +is( $h->dump, + $h->perl, + '... the value is correctly dumped with perl()' ); diff --git a/t/005_string.t b/t/005_string.t index bdba48f..ef18efb 100644 --- a/t/005_string.t +++ b/t/005_string.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 18; +use Test::More tests => 20; use Test::Exception; BEGIN { @@ -12,6 +12,8 @@ BEGIN { use Moose::Autobox; +my $VAR1; # for eval of dumps + is('Hello World'->lc, 'hello world', '... $str->lc'); is('Hello World'->uc, 'HELLO WORLD', '... $str->uc'); @@ -45,3 +47,9 @@ is('Hello World, Hello'->rindex('Hello'), 13, '... got the correct right index') is('Hello World, Hello'->rindex('Hello', 6), 0, '... got the correct right index'); +eval 'Hello World, Hello'->dump; +is($VAR1, 'Hello World, Hello' , '... eval of &dump works'); + +eval 'Hello World, Hello'->perl; +is($VAR1, 'Hello World, Hello' , '... eval of &perl works'); +