autobox-List-Util-20090629000755000765000024 011222221503 15044 5ustar00cowensstaff000000000000Build.PL000444000765000024 104511222221503 16416 0ustar00cowensstaff000000000000autobox-List-Util-20090629use strict; use warnings; use Module::Build; my $builder = Module::Build->new( module_name => 'autobox::List::Util', license => 'perl', dist_author => 'Chas. J. Owens IV ', dist_version_from => 'lib/autobox/List/Util.pm', build_requires => { 'Test::More' => 0, 'Module::Load' => 0, 'List::Util' => 0, 'autobox' => 0, }, add_to_cleanup => [ 'autobox-List-Util-*' ], create_makefile_pl => 'traditional', ); $builder->create_build_script(); Changes000444000765000024 12511222221503 16373 0ustar00cowensstaff000000000000autobox-List-Util-20090629Revision history for autobox-List-Util 20090429 20090430/0338 First cut Makefile.PL000444000765000024 105311222221503 17073 0ustar00cowensstaff000000000000autobox-List-Util-20090629# Note: this file was auto-generated by Module::Build::Compat version 0.33 use ExtUtils::MakeMaker; WriteMakefile ( 'NAME' => 'autobox::List::Util', 'VERSION_FROM' => 'lib/autobox/List/Util.pm', 'PREREQ_PM' => { 'List::Util' => 0, 'Module::Load' => 0, 'Test::More' => 0, 'autobox' => 0 }, 'INSTALLDIRS' => 'site', 'EXE_FILES' => [], 'PL_FILES' => {} ) ; MANIFEST000444000765000024 27011222221503 16232 0ustar00cowensstaff000000000000autobox-List-Util-20090629Build.PL Changes lib/autobox/List/Util.pm MANIFEST MANIFEST.SKIP README t/00-load.t t/first.t t/max.t t/maxstr.t t/min.t t/minstr.t t/reduce.t t/shuffle.t t/sum.t Makefile.PL META.yml MANIFEST.SKIP000444000765000024 114311222221503 17017 0ustar00cowensstaff000000000000autobox-List-Util-20090629#!start included /usr/local/perl/5.10.0/lib/5.10.0/ExtUtils/MANIFEST.SKIP # Avoid version control files. \bRCS\b \bCVS\b \bSCCS\b ,v$ \B\.svn\b \B\.git\b \B\.gitignore\b \b_darcs\b # Avoid Makemaker generated and utility files. \bMANIFEST\.bak \bMakefile$ \bblib/ \bMakeMaker-\d \bpm_to_blib\.ts$ \bpm_to_blib$ \bblibdirs\.ts$ # 6.18 through 6.25 generated this # Avoid Module::Build generated and utility files. \bBuild$ \b_build/ # Avoid temp and backup files. ~$ \.old$ \#$ \b\.# \.bak$ # Avoid Devel::Cover files. \bcover_db\b #!end included /usr/local/perl/5.10.0/lib/5.10.0/ExtUtils/MANIFEST.SKIP META.yml000444000765000024 100611222221503 16370 0ustar00cowensstaff000000000000autobox-List-Util-20090629--- name: autobox-List-Util version: 20090629 author: - 'Chas. J. Owens IV ' abstract: bring the List::Util functions to autobox license: perl resources: license: http://dev.perl.org/licenses/ build_requires: List::Util: 0 Module::Load: 0 Test::More: 0 autobox: 0 provides: autobox::List::Util: file: lib/autobox/List/Util.pm version: 20090629 generated_by: Module::Build version 0.33 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 README000444000765000024 105611222221503 16004 0ustar00cowensstaff000000000000autobox-List-Util-20090629autobox-List-Util Make all of the functions in List::Util into methods that can be called on arrays and arrayrefs INSTALLATION To install this module, run the following commands: perl Build.PL ./Build ./Build test ./Build install SUPPORT AND DOCUMENTATION After installing, you can find documentation for this module with the perldoc command. perldoc autobox::List::Util COPYRIGHT AND LICENCE Copyright (C) 2009 Chas. J. Owens IV This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. lib000755000765000024 011222221503 15533 5ustar00cowensstaff000000000000autobox-List-Util-20090629autobox000755000765000024 011222221503 17214 5ustar00cowensstaff000000000000autobox-List-Util-20090629/libList000755000765000024 011222221503 20127 5ustar00cowensstaff000000000000autobox-List-Util-20090629/lib/autoboxUtil.pm000444000765000024 710111222221503 21536 0ustar00cowensstaff000000000000autobox-List-Util-20090629/lib/autobox/Listpackage autobox::List::Util; use warnings; use strict; use base 'autobox'; sub import { my $class = shift; $class->SUPER::import(ARRAY => 'autobox::List::Util::_private'); } package autobox::List::Util::_private; use strict; use warnings; use Module::Load; sub first { load List::Util; my ($self, $coderef) = @_; return List::Util::first { $coderef->() } @$self } sub max { load List::Util; my $self = shift; return List::Util::max @$self } sub maxstr { load List::Util; my $self = shift; return List::Util::maxstr @$self } sub min { load List::Util; my $self = shift; return List::Util::min @$self } sub minstr { load List::Util; my $self = shift; return List::Util::minstr @$self } sub reduce { load List::Util; my ($self, $coderef) = @_; return List::Util::reduce { #FIXME: this needs to know the package we are exporting to local ($main::a, $main::b) = ($a, $b); $coderef->(); } @$self } sub shuffle { load List::Util; my $self = shift; return List::Util::shuffle @$self if wantarray; return [ List::Util::shuffle @$self ]; } sub sum { load List::Util; my $self = shift; return List::Util::sum @$self } package autobox::List::Util; =head1 NAME autobox::List::Util - bring the List::Util functions to autobox =head1 VERSION Version 20090629 =cut our $VERSION = '20090629'; =head1 SYNOPSIS C brings all of the functions from List::Util to arrays as methods. use autobox::List::Util; my @array = qw/ foo bar baz /; print @array->first(sub { /ar/ }), "\n"; # "bar" print [5, 6, 3, 4]->max, "\n"; # 6 print @array->maxstr, "\n"; # baz print [5, 6, 3, 4]->min, "\n"; # 3 print @array->minstr, "\n"; # foo print [1 .. 10]->shuffle, "\n"; #1 to 10 randomly shuffled print [1 .. 10]->sum, "\n"; # 55 print [1 .. 10]->reduce( sub { $a + $b } ), "\n"; # 55 =head1 METHODS =head2 first(coderef) This method behaves nearly the same as the first function from List::Util, but it takes a coderef not a block because methods can't use prototypes. =head2 reduce(coderef) This method behaves nearly the same as the reduce function from List::Util, but it takes a coderef not a block for the same reason. It also has a bug (see L) =head2 shuffle If called in scalar context it returns a reference to an array instead of a list. This allows shuffle to be chained with other calls. =head2 max, maxstr, min, minstr, sum These methods behave exactly the same as their List::Util counterparts. =head1 AUTHOR Chas. J. Owens IV, C<< >> =head1 BUGS The reduce method works with $main::a and $main::b, not your current package's $a and $b, so you need to say print @array->reduce( sub { $main::a + $main::b } ), "\n"; if you are not in the main package. Reduce uses $_, so it doesn't suffer from this problem. =head1 SUPPORT You can find documentation for this module with the perldoc command. perldoc autobox::List::Util You can also look for information at: =over 4 =item * RT: CPAN's request tracker L =item * AnnoCPAN: Annotated CPAN documentation L =item * CPAN Ratings L =item * Search CPAN L =back =head1 ACKNOWLEDGEMENTS =head1 COPYRIGHT & LICENSE Copyright 2009 Chas. J. Owens IV, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut 1; # End of autobox::List::Util t000755000765000024 011222221503 15230 5ustar00cowensstaff000000000000autobox-List-Util-2009062900-load.t000444000765000024 24611222221503 16670 0ustar00cowensstaff000000000000autobox-List-Util-20090629/tuse blib; use Test::More tests => 1; BEGIN { use_ok( 'autobox::List::Util' ); } diag( "Testing autobox::List::Util $autobox::List::Util::VERSION, Perl $], $^X" ); first.t000555000765000024 402611222221503 16706 0ustar00cowensstaff000000000000autobox-List-Util-20090629/tuse blib; use strict; use warnings; use autobox::List::Util; use Test::More tests => 13; my $v = [9,4,5,6]->first( sub { 8 == ($_ - 1) } ); is($v, 9, 'one more than 8'); $v = [1,2,3,4]->first( sub { 0 } ); is($v, undef, 'none match'); $v = []->first( sub { 0 } ); is($v, undef, 'no args'); $v = [[qw(a b c)], [qw(d e f)], [qw(g h i)]]->first( sub { $_->[1] le "e" and "e" le $_->[2] } ); is_deeply($v, [qw(d e f)], 'reference args'); # Check that eval{} inside the block works correctly my $i = 0; $v = [0,1,2,3,4,5,5]->first( sub { eval { die }; ($i == 5, $i = $_)[0] } ); is($v, 5, 'use of eval'); $v = eval { [0,0,1]->first( sub { die if $_ } ) }; is($v, undef, 'use of die'); sub foobar { ["not ","not ","not "]->first( sub { !defined(wantarray) || wantarray } ) } ($v) = foobar(); is($v, undef, 'wantarray'); # Can we leave the sub with 'return'? $v = [2,4,6,12]->first( sub {return ($_>6)} ); is($v, 12, 'return'); # ... even in a loop? $v = [2,4,6,12]->first( sub {while(1) {return ($_>6)} } ); is($v, 12, 'return from loop'); # Does it work from another package? { package Foo; use autobox::List::Util; ::is( [1..4,24]->first(sub{$_>4}), 24, 'other package'); } # Can we undefine a first sub while it's running? sub self_immolate {undef &self_immolate; 1} eval { $v = [1,2]->first(\&self_immolate) }; like($@, qr/^Can't undef active subroutine/, "undef active sub"); # Redefining an active sub should not fail, but whether the # redefinition takes effect immediately depends on whether we're # running the Perl or XS implementation. { local $SIG{__WARN__} = sub {}; #trap warnings; sub self_updating { local $^W; *self_updating = sub{1} ;1} eval { $v = [1,2]->first(\&self_updating) }; is($@, '', 'redefine self'); } { my $failed = 0; sub rec { my $n = shift; if (!defined($n)) { # No arg means we're being called by first() return 1; } if ($n<5) { rec($n+1); } else { $v = [1,2]->first(\&rec) } $failed = 1 if !defined $n; } rec(1); ok(!$failed, 'from active sub'); } max.t000555000765000024 52511222221503 16324 0ustar00cowensstaff000000000000autobox-List-Util-20090629/tuse blib; use strict; use autobox::List::Util; use Test::More tests => 4; my $v; $v = [1]->max; is($v, 1, 'single arg'); $v = [1,2]->max; is($v, 2, '2-arg ordered'); $v = [2,1]->max; is($v, 2, '2-arg reverse ordered'); my @a = map { rand() } 1 .. 20; my @b = sort { $a <=> $b } @a; $v = @a->max; is($v, $b[-1], '20-arg random order'); maxstr.t000555000765000024 65011222221503 17054 0ustar00cowensstaff000000000000autobox-List-Util-20090629/tuse blib; use strict; use autobox::List::Util; use Test::More tests => 4; my $v; $v = ['a']->maxstr; is($v, 'a', 'single arg'); $v = ['a','b']->maxstr; is($v, 'b', '2-arg ordered'); $v = ['B','A']->maxstr; is($v, 'B', '2-arg reverse ordered'); my @a = map { pack("u", pack("C*",map { int(rand(256))} (0..int(rand(10) + 2)))) } 0 .. 20; my @b = sort { $a cmp $b } @a; $v = @a->maxstr; is($v, $b[-1], 'random ordered'); min.t000555000765000024 53411222221503 16322 0ustar00cowensstaff000000000000autobox-List-Util-20090629/tuse blib; use strict; use autobox::List::Util; use Test::More tests => 4; my $v; $v = [(9)]->min; is($v, 9, 'single arg'); $v = [1,2]->min; is($v, 1, '2-arg ordered'); $v = [(2,1)]->min; is($v, 1, '2-arg reverse ordered'); my @a = map { rand() } 1 .. 20; my @b = sort { $a <=> $b } @a; $v = [(@a)]->min; is($v, $b[0], '20-arg random order'); minstr.t000555000765000024 66111222221503 17054 0ustar00cowensstaff000000000000autobox-List-Util-20090629/tuse blib; use strict; use autobox::List::Util; use Test::More tests => 4; my $v; $v = [('a')]->minstr; is($v, 'a', 'single arg'); $v = [('a','b')]->minstr; is($v, 'a', '2-arg ordered'); $v = [('B','A')]->minstr; is($v, 'A', '2-arg reverse ordered'); my @a = map { pack("u", pack("C*",map { int(rand(256))} (0..int(rand(10) + 2)))) } 0 .. 20; my @b = sort { $a cmp $b } @a; $v = [(@a)]->minstr; is($v, $b[0], 'random ordered'); reduce.t000555000765000024 474011222221503 17031 0ustar00cowensstaff000000000000autobox-List-Util-20090629/tuse blib; use strict; use autobox::List::Util; use Test::More tests => 19; my $v = []->reduce(sub {}); is( $v, undef, 'no args'); $v = [756,3,7,4]->reduce(sub { $a / $b }); is( $v, 9, '4-arg divide'); $v = [6]->reduce( sub { $a / $b } ); is( $v, 6, 'one arg'); my @a = map { rand } 0 .. 20; $v = @a->reduce( sub { $a < $b ? $a : $b }); is( $v, @a->min, 'min'); @a = map { pack("C", int(rand(256))) } 0 .. 20; $v = @a->reduce( sub { $a . $b } ); is( $v, join("",@a), 'concat'); sub add { my($aa, $bb) = @_; return $aa + $bb; } $v = [3, 2, 1]->reduce( sub { my $t="$a $b\n"; 0+add($a, $b) } ); is( $v, 6, 'call sub'); # Check that eval{} inside the block works correctly $v = [0,1,2,3,4]->reduce( sub { eval { die }; $a + $b } ); is( $v, 10, 'use eval{}'); $v = !defined eval { [0..4]->reduce( sub { die if $b > 2; $a + $b } ) }; ok($v, 'die'); sub foobar { [0..3]->reduce( sub { (defined(wantarray) && !wantarray) ? $a+1 : 0 } ) } ($v) = foobar(); is( $v, 3, 'scalar context'); sub add2 { $a + $b } $v = [1,2,3]->reduce(\&add2); is( $v, 6, 'sub reference'); $v = [3,4,5]->reduce(sub { add2() }); is( $v, 12, 'call sub'); $v = [1,2,3]->reduce( sub { eval "$a + $b" } ); is( $v, 6, 'eval string'); $a = 8; $b = 9; $v = [1,2,3]->reduce(sub { $a * $b }); is( $a, 8, 'restore $a'); is( $b, 9, 'restore $b'); # Can we leave the sub with 'return'? $v = [2,4,6]->reduce( sub {return $a+$b} ); is($v, 12, 'return'); # ... even in a loop? $v = [2,4,6]->reduce(sub {while(1) {return $a+$b} }); is($v, 12, 'return from loop'); # Does it work from another package? # FIXME: this doesn't work #{ # package Foo; # $a = $b; # ::is([1..4]->reduce( sub {$a*$b} ), 24, 'other package'); #} # Can we undefine a reduce sub while it's running? sub self_immolate {undef &self_immolate; 1} eval { $v = [1,2]->reduce(\&self_immolate) }; like($@, qr/^Can't undef active subroutine/, "undef active sub"); # Redefining an active sub should not fail, but whether the # redefinition takes effect immediately depends on whether we're # running the Perl or XS implementation. sub self_updating { local $^W; *self_updating = sub{1} ;1 } eval { $v = [1,2]->reduce(\&self_updating) }; is($@, '', 'redefine self'); { my $failed = 0; sub rec { my $n = shift; if (!defined($n)) { # No arg means we're being called by reduce() return 1; } if ($n<5) { rec($n+1); } else { $v = [1,2]->reduce(\&rec) } $failed = 1 if !defined $n; } rec(1); ok(!$failed, 'from active sub'); } shuffle.t000555000765000024 137011222221503 17212 0ustar00cowensstaff000000000000autobox-List-Util-20090629/tuse blib; use strict; use autobox::List::Util; use Test::More tests => 13; my @r; @r = []->shuffle; ok( !@r, 'no args'); @r = [9]->shuffle; is( 0+@r, 1, '1 in 1 out'); is( $r[0], 9, 'one arg'); my @in = 1..100; @r = @in->shuffle; is( 0+@r, 0+@in, 'arg count'); isnt( "@r", "@in", 'result different to args'); my @s = sort { $a <=> $b } @r; is( "@in", "@s", 'values'); my $r; $r = []->shuffle; ok( !@$r, 'no args (ref)'); $r = [9]->shuffle; is( 0+@$r, 1, '1 in 1 out (ref)'); is( $r->[0], 9, 'one arg (ref)'); $r = @in->shuffle; is( 0+@$r, 0+@in, 'arg count (ref)'); isnt( "@$r", "@in", 'result different to args (ref)'); @s = sort { $a <=> $b } @$r; is( "@in", "@s", 'values (ref)'); is @in->shuffle->first(sub { $_ == 5 }), 5, "can chain calls"; sum.t000555000765000024 56011222221503 16342 0ustar00cowensstaff000000000000autobox-List-Util-20090629/tuse blib; use Test::More tests => 6; use strict; use autobox::List::Util; my $v = []->sum; is( $v, undef, 'no args'); $v = [(9)]->sum; is( $v, 9, 'one arg'); $v = [(1,2,3,4)]->sum; is( $v, 10, '4 args'); $v = [(-1)]->sum; is( $v, -1, 'one -1'); my $x = -3; $v = [($x, 3)]->sum; is( $v, 0, 'variable arg'); $v = [(-3.5,3)]->sum; is( $v, -0.5, 'real numbers');