File Coverage

blib/lib/autobox/List/Util.pm
Criterion Covered Total %
statement 48 48 100.0
branch 2 2 100.0
condition n/a
subroutine 17 17 100.0
pod n/a
total 67 67 100.0


line stmt bran cond sub pod time code
1             package autobox::List::Util;
2              
3 9     9   53217 use warnings;
  9         16  
  9         568  
4 9     9   47 use strict;
  9         13  
  9         271  
5              
6 9     9   45 use base 'autobox';
  9         16  
  9         10419  
7              
8             sub import {
9 10     10   28508 my $class = shift;
10 10         95 $class->SUPER::import(ARRAY => 'autobox::List::Util::_private');
11             }
12              
13             package autobox::List::Util::_private;
14              
15 9     9   103361 use strict;
  9         26  
  9         261  
16 9     9   42 use warnings;
  9         20  
  9         288  
17 9     9   9595 use Module::Load;
  9         11587  
  9         57  
18              
19             sub first {
20 14     14   6715 load List::Util;
21 14         762 my ($self, $coderef) = @_;
22 14     126   94 return List::Util::first { $coderef->() } @$self
  126         443  
23             }
24              
25             sub max {
26 4     4   2060 load List::Util;
27 4         252 my $self = shift;
28 4         28 return List::Util::max @$self
29             }
30              
31             sub maxstr {
32 4     4   2042 load List::Util;
33 4         219 my $self = shift;
34 4         20 return List::Util::maxstr @$self
35             }
36              
37             sub min {
38 5     5   2273 load List::Util;
39 5         296 my $self = shift;
40 5         40 return List::Util::min @$self
41             }
42              
43             sub minstr {
44 4     4   1632 load List::Util;
45 4         262 my $self = shift;
46 4         29 return List::Util::minstr @$self
47             }
48              
49             sub reduce {
50 18     18   6378 load List::Util;
51 18         765 my ($self, $coderef) = @_;
52             return List::Util::reduce {
53             #FIXME: this needs to know the package we are exporting to
54 70     70   255 local ($main::a, $main::b) = ($a, $b);
55 70         114 $coderef->();
56 18         93 } @$self
57             }
58              
59             sub shuffle {
60 7     7   4847 load List::Util;
61 7         388 my $self = shift;
62 7 100       90 return List::Util::shuffle @$self if wantarray;
63 4         63 return [ List::Util::shuffle @$self ];
64             }
65              
66             sub sum {
67 6     6   1612 load List::Util;
68 6         275 my $self = shift;
69 6         28 return List::Util::sum @$self
70             }
71              
72             package autobox::List::Util;
73              
74             =head1 NAME
75              
76             autobox::List::Util - bring the List::Util functions to autobox
77              
78             =head1 VERSION
79              
80             Version 20090629
81              
82             =cut
83              
84             our $VERSION = '20090629';
85              
86             =head1 SYNOPSIS
87              
88             C brings all of the functions from List::Util
89             to arrays as methods.
90              
91             use autobox::List::Util;
92              
93             my @array = qw/ foo bar baz /;
94              
95             print @array->first(sub { /ar/ }), "\n"; # "bar"
96              
97             print [5, 6, 3, 4]->max, "\n"; # 6
98              
99             print @array->maxstr, "\n"; # baz
100            
101             print [5, 6, 3, 4]->min, "\n"; # 3
102              
103             print @array->minstr, "\n"; # foo
104              
105             print [1 .. 10]->shuffle, "\n"; #1 to 10 randomly shuffled
106              
107             print [1 .. 10]->sum, "\n"; # 55
108              
109             print [1 .. 10]->reduce( sub { $a + $b } ), "\n"; # 55
110              
111             =head1 METHODS
112              
113             =head2 first(coderef)
114              
115             This method behaves nearly the same as the first function from List::Util,
116             but it takes a coderef not a block because methods can't use prototypes.
117              
118             =head2 reduce(coderef)
119              
120             This method behaves nearly the same as the reduce function from List::Util,
121             but it takes a coderef not a block for the same reason. It also has a bug
122             (see L)
123              
124             =head2 shuffle
125              
126             If called in scalar context it returns a reference to an array instead
127             of a list. This allows shuffle to be chained with other calls.
128              
129             =head2 max, maxstr, min, minstr, sum
130              
131             These methods behave exactly the same as their List::Util counterparts.
132              
133             =head1 AUTHOR
134              
135             Chas. J. Owens IV, C<< >>
136              
137             =head1 BUGS
138              
139             The reduce method works with $main::a and $main::b, not your current
140             package's $a and $b, so you need to say
141              
142             print @array->reduce( sub { $main::a + $main::b } ), "\n";
143              
144             if you are not in the main package. Reduce uses $_, so it doesn't
145             suffer from this problem.
146              
147             =head1 SUPPORT
148              
149             You can find documentation for this module with the perldoc command.
150              
151             perldoc autobox::List::Util
152              
153              
154             You can also look for information at:
155              
156             =over 4
157              
158             =item * RT: CPAN's request tracker
159              
160             L
161              
162             =item * AnnoCPAN: Annotated CPAN documentation
163              
164             L
165              
166             =item * CPAN Ratings
167              
168             L
169              
170             =item * Search CPAN
171              
172             L
173              
174             =back
175              
176              
177             =head1 ACKNOWLEDGEMENTS
178              
179              
180             =head1 COPYRIGHT & LICENSE
181              
182             Copyright 2009 Chas. J. Owens IV, all rights reserved.
183              
184             This program is free software; you can redistribute it and/or modify it
185             under the same terms as Perl itself.
186              
187              
188             =cut
189              
190             1; # End of autobox::List::Util