File Coverage

blib/lib/List/StackBy.pm
Criterion Covered Total %
statement 21 21 100.0
branch 2 2 100.0
condition 5 9 55.5
subroutine 5 5 100.0
pod 1 1 100.0
total 34 38 89.4


line stmt bran cond sub pod time code
1             package List::StackBy;
2            
3 2     2   149259 use 5.020000;
  2         15  
4 2     2   13 use strict;
  2         4  
  2         56  
5 2     2   13 use warnings;
  2         3  
  2         73  
6 2     2   12 use base qw(Exporter);
  2         4  
  2         759  
7            
8             our $VERSION = '0.01';
9            
10             our %EXPORT_TAGS = ( 'all' => [ qw(
11             stack_by
12             ) ] );
13            
14             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
15            
16             our @EXPORT = qw(
17             stack_by
18             );
19            
20             sub stack_by(&@) {
21 2     2 1 701 my $code = shift;
22 2         3 my @result;
23             my $prev_key;
24            
25 2         5 for (@_) {
26 10         14 my $cur_key = $code->( $_ );
27            
28 10 100 66     67 if (not(ref $prev_key)
      33        
      66        
29             or not(defined $cur_key)
30             or not(defined $$prev_key)
31             or not($cur_key eq $$prev_key)) {
32            
33 7         11 push @result, [];
34             }
35            
36 10         10 push @{ $result[-1] }, $_;
  10         17  
37            
38 10         13 $prev_key = \$cur_key;
39             }
40            
41 2         7 return @result;
42             }
43            
44             1;
45            
46             __END__