File Coverage

blib/lib/Data/Rand.pm
Criterion Covered Total %
statement 40 57 70.1
branch 10 24 41.6
condition 6 15 40.0
subroutine 6 9 66.6
pod 4 4 100.0
total 66 109 60.5


line stmt bran cond sub pod time code
1             package Data::Rand;
2              
3 1     1   24159 use warnings;
  1         2  
  1         29  
4 1     1   4 use strict;
  1         2  
  1         32  
5              
6 1     1   916 use version; our $VERSION = qv('0.0.4');
  1         2315  
  1         6  
7              
8 1     1   78 use base 'Exporter';
  1         2  
  1         749  
9             our @EXPORT = qw( rand_data );
10             our @EXPORT_OK = qw( rand_data_string rand_data_array );
11              
12             sub seed_calc_with {
13 0     0 1 0 require Time::HiRes;
14 0         0 my ($sec, $mic) = Time::HiRes::gettimeofday();
15 0   0     0 srand($sec ^ $mic ^ $$ ^ shift() || rand( 999_999_999_999_999 ));
16             }
17              
18             sub rand_data {
19 100002 50   100002 1 45928851 my $options_hr = ref $_[-1] eq 'HASH' ? pop @_ : {}; # last item a hashref or not
20 100002         173275 %{ $options_hr->{'details'} } = ();
  100002         374992  
21            
22 100002         159069 my ( $size, $items_ar ) = @_;
23              
24 100002 50 66     321487 $size = 32 if !defined $size || $size eq '0' || $size !~ m{ \A \d+ \z }xms;
      66        
25 100002         3274560 $options_hr->{'details'}{'size'} = $size;
26              
27 100002         1380977 my @items = ( 0 .. 9, 'A' .. 'Z', 'a' .. 'z' );
28 100002         230219 $options_hr->{'details'}{'using_default_list'} = 1;
29              
30 100002 100       243206 if ( ref $items_ar eq 'ARRAY' ) {
31 2 50       4 if ( @{ $items_ar } ) {
  2         8  
32 2         3 @items = @{ $items_ar };
  2         15  
33 2         7 $options_hr->{'details'}{'using_default_list'} = 0;
34             }
35             }
36 100002         12766134 $options_hr->{'details'}{'items'} = [ @items ];
37              
38 100002 50 33     322550 if ( $options_hr->{'use_unique_list'} && !$options_hr->{'details'}{'using_default_list'} ) {
39 0         0 my %uniq;
40 0 0       0 @items = map { $uniq{$_}++ == 0 ? $_ : () } @items; # see List::MoreUtils::uniq() #left prec, reverse @_ right prec
  0         0  
41             }
42              
43 100002 50 33     282613 $size = @items if $size > @items && $options_hr->{'do_not_repeat_index'};
44 100002         147078 my $len = scalar @items;
45            
46 100002     3200002   452375 my $get_random_index = sub { rand shift() };
  3200002         27283507  
47            
48 100002         223245 $options_hr->{'details'}{'using_default_index_picker'} = 1;
49 100002 50       253048 if ( ref $options_hr->{'get_random_index'} eq 'CODE' ) {
50 0         0 $options_hr->{'details'}{'using_default_index_picker'} = 0;
51 0         0 $get_random_index = $options_hr->{'get_random_index'};
52             }
53            
54 100002         122405 my @data;
55             my %used;
56            
57             PART:
58 100002         227887 for ( 1 .. $size ) {
59 3200002         5422399 my $index = int( $get_random_index->($len) ); # negatives ok so no abs()
60 3200002 50       6680985 if( $options_hr->{'do_not_repeat_index'} ) {
61 0         0 my $try = 0;
62             TRY:
63 0         0 while ( exists $used{ $index } ) {
64 0         0 $try++;
65 0         0 $index = int( $get_random_index->($len) ); # negatives ok so no abs()
66 0 0       0 last TRY if $try > $size; # keep a custom index fetcher from causing infinite loop
67             }
68 0         0 $used{ $index }++;
69             }
70              
71 3200002         7543887 push @data, $items[ $index ];
72             }
73              
74 100002 50       2488936 return wantarray ? @data : join('', @data);
75             }
76              
77             sub rand_data_string {
78 0     0 1   return scalar( rand_data(@_) );
79             }
80              
81             sub rand_data_array {
82 0     0 1   my @rand = rand_data(@_);
83 0 0         return wantarray ? @rand : \@rand;
84             }
85              
86             1;
87              
88             __END__