File Coverage

blib/lib/Petal/Utils/Limitr.pm
Criterion Covered Total %
statement 38 38 100.0
branch 5 8 62.5
condition 3 9 33.3
subroutine 8 8 100.0
pod 0 2 0.0
total 54 65 83.0


line stmt bran cond sub pod time code
1             package Petal::Utils::Limitr;
2              
3 4     4   24 use strict;
  4         8  
  4         225  
4 4     4   24 use warnings::register;
  4         8  
  4         494  
5              
6 4     4   22 use Carp;
  4         8  
  4         340  
7              
8 4     4   21 use base qw( Petal::Utils::Base );
  4         6  
  4         356  
9              
10 4     4   21 use constant name => 'limitr';
  4         8  
  4         239  
11 4     4   22 use constant aliases => qw();
  4         7  
  4         1580  
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 121685 my $class = shift;
18 2         4 my $hash = shift;
19 2   33     9 my $args = shift || confess( "'limitr' expects 2 variables (got nothing)!" );
20              
21 2         16 my @args = $class->split_args( $args );
22 2   33     8 my $key = $args[0] || confess( "1st arg to 'limit' should be an array (got nothing)!" );
23 2   33     7 my $count = $args[1] || confess( "2nd arg to 'limit' should be a variable (got nothing)!" );
24              
25 2         10 my $arrayref = $hash->fetch($key);
26             # Shuffle full array
27 2         149 fisher_yates_shuffle($arrayref);
28 2         4 $count--;
29             # trim $count to max size of array
30 2 50       8 $count = $#$arrayref if $#$arrayref < $count;
31 2 50       16 return [] if $count < 0;
32 2         5 return [@{$arrayref}[0 .. $count]];
  2         12  
33              
34             }
35              
36              
37             # Generate a random permutation of @array in place
38             # Usage: fisher_yates_shuffle( \@array ) :
39             sub fisher_yates_shuffle {
40 2     2 0 4 my $array = shift;
41 2 50       8 return unless $#$array >= 0;
42 2         3 my $i;
43 2         8 for ($i = @$array; --$i; ) {
44 4         61 my $j = int rand ($i+1);
45 4 100       12 next if $i == $j;
46 3         16 @$array[$i,$j] = @$array[$j,$i];
47             }
48             }
49              
50              
51             1;
52              
53             __END__