File Coverage

blib/lib/Petal/Utils/Limit.pm
Criterion Covered Total %
statement 30 30 100.0
branch 2 4 50.0
condition 3 9 33.3
subroutine 7 7 100.0
pod 0 1 0.0
total 42 51 82.3


line stmt bran cond sub pod time code
1             package Petal::Utils::Limit;
2              
3 4     4   22 use strict;
  4         45  
  4         184  
4 4     4   21 use warnings::register;
  4         7  
  4         528  
5              
6 4     4   24 use Carp;
  4         7  
  4         312  
7              
8 4     4   29 use base qw( Petal::Utils::Base );
  4         8  
  4         340  
9              
10 4     4   20 use constant name => 'limit';
  4         7  
  4         240  
11 4     4   22 use constant aliases => qw();
  4         6  
  4         1295  
12              
13             our $VERSION = ((require Petal::Utils), $Petal::Utils::VERSION)[1];
14             our $REVISION = (split(/ /, ' $Revision: 1.2 $ '))[2];
15              
16             sub process {
17 2     2 0 28698 my $class = shift;
18 2         4 my $hash = shift;
19 2   33     7 my $args = shift || confess( "'limit' expects 2 variables (got nothing)!" );
20              
21 2         16 my @args = $class->split_args( $args );
22 2   33     9 my $key = $args[0] || confess( "1st arg to 'limit' should be an array (got nothing)!" );
23 2   33     5 my $count = $args[1] || confess( "2nd arg to 'limit' should be a variable (got nothing)!" );
24              
25 2         11 my $arrayref = $hash->fetch($key);
26 2         127 $count--;
27             # trim $count to max size of array
28 2 50       7 $count = $#$arrayref if $#$arrayref < $count;
29 2 50       7 return [] if $count < 0;
30 2         4 return [@{$arrayref}[0 .. $count]];
  2         11  
31             }
32              
33             1;
34              
35             __END__