File Coverage

blib/lib/Array/Group.pm
Criterion Covered Total %
statement 29 31 93.5
branch 11 16 68.7
condition n/a
subroutine 6 6 100.0
pod 0 2 0.0
total 46 55 83.6


line stmt bran cond sub pod time code
1             package Array::Group;
2              
3 1     1   19581 use 5.006;
  1         4  
4              
5 1     1   5 use strict;
  1         2  
  1         20  
6              
7 1     1   5 use Carp;
  1         5  
  1         599  
8              
9             require Exporter;
10              
11             our @ISA = qw(Exporter);
12              
13             # Items to export into callers namespace by default. Note: do not export
14             # names by default without a very good reason. Use EXPORT_OK instead.
15             # Do not simply export all your public functions/methods/constants.
16              
17             # This allows declaration use Array::Group ':all';
18             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
19             # will save memory.
20             our %EXPORT_TAGS = ( 'all' => [ qw(
21             ngroup
22             dissect
23             ) ] );
24              
25             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
26              
27             our @EXPORT = qw();
28              
29             our $VERSION = '4.2';
30              
31             # Preloaded methods go here.
32             sub ngroup {
33 4     4 0 1102 my ($size, $r_list) = &_validate_params;
34              
35 4         5 my @list = @{$r_list};
  4         9  
36 4         5 my @lol;
37              
38 4         29 push @lol, [splice @list, 0, $size] while @list;
39              
40 4 100       21 return wantarray ? @lol : \@lol;
41             }
42              
43             sub dissect {
44 3     3 0 657 my ($size, $r_list) = &_validate_params;
45              
46 3         6 my @lol;
47 3         4 my ($i, $j) = (0, 0);
48              
49 3         6 foreach (@$r_list) {
50 26         39 $lol[$i]->[$j] = $_;
51 26 100       50 $i = 0, $j++ unless (++$i % $size);
52             }
53              
54 3 50       16 return wantarray ? @lol : \@lol;
55             }
56              
57              
58             # Internal parameter validation function
59             sub _validate_params {
60             # Check we've been called with at least one argument
61 7 50   7   16 Carp::confess( "Called with no arguments" ) if $#_ == -1;
62              
63             # First param might be a class (if invoked as a class method). Discard it if so.
64 7 100       19 shift if $_[0] =~ /^[a-zA-Z0-9]+ (?: :: [a-zA-Z0-9]+ )$/x;
65              
66             # Check we have at least 2 arguments remaining
67 7 50       15 Carp::confess( "Called with insufficient arguments" ) if( $#_ < 1 );
68              
69             # Next argument is size. check it is a valid positive integer.
70 7         14 my $size = shift;
71 7 50       27 if( $size !~ /^\+?\d+$/ ) {
    50          
72 0         0 Carp::confess( "Size '$size' is not a valid positive integer" );
73             } elsif( $size == 0 ) {
74 0         0 Carp::confess( "'$size' is an invalid array size" );
75             }
76              
77             # If only one argument remains, check to see if it is an arrayref, otherwise, reate a reference to it
78 7         7 my $r_list;
79             # if( ($#_ == 0) &&
80             # (ref($_[0]) eq 'ARRAY') ) {
81 7         8 $r_list = $_[0];
82             # } else {
83             # $r_list = \@_;
84             # }
85              
86 7         13 return $size, $r_list;
87             }
88              
89             # Autoload methods go after =cut, and are processed by the autosplit program.
90              
91             1;
92             __END__