File Coverage

blib/lib/Data/Randr.pm
Criterion Covered Total %
statement 30 30 100.0
branch 11 12 91.6
condition 2 8 25.0
subroutine 8 8 100.0
pod 4 4 100.0
total 55 62 88.7


line stmt bran cond sub pod time code
1             package Data::Randr;
2 2     2   42747 use strict;
  2         4  
  2         53  
3 2     2   12 use warnings;
  2         4  
  2         59  
4 2     2   12 use Carp qw/croak/;
  2         11  
  2         131  
5 2     2   9 use Exporter 'import';
  2         4  
  2         799  
6             our @EXPORT_OK = qw/randr/;
7              
8             our $VERSION = '0.01';
9              
10             sub new {
11 4     4 1 2391 my ($class, %args) = @_;
12              
13 4         9 my $rate = delete $args{rate};
14 4         8 my $digit = delete $args{digit};
15              
16 4         19 bless {
17             rate => $rate,
18             digit => $digit,
19             }, $class;
20             }
21              
22 20000     20000 1 31480 sub rate { $_[0]->{rate} }
23 30000     30000 1 46298 sub digit { $_[0]->{digit} }
24              
25             sub randr {
26 90000     90000 1 424002 my ($self, $base, $rate, $digit);
27              
28 90000 100       172313 if (ref $_[0] eq __PACKAGE__) {
29 40000         60426 ($self, $base, $rate, $digit) = @_;
30 40000 100       76165 $rate = $rate ? $rate : $self->rate;
31 40000 100       87609 $digit = $digit ? $digit : $self->digit;
32             }
33             else {
34 50000         77181 ($base, $rate, $digit) = @_;
35             }
36              
37 90000 50 0     555658 croak "required rate(0 - 100) :".($rate || '') if !$rate || $rate < 0 || $rate > 100;
      33        
      33        
38              
39 90000         132310 my $splash = int( $base * ($rate/100) );
40 90000 100       187290 my $result = $base - $splash + rand($splash*2+1*($digit ? 0 : 1));
41              
42 90000 100       155927 if ($digit) {
43 30000         145409 return sprintf("%0.${digit}f", $result);
44             }
45              
46 60000         114949 return int($result);
47             }
48              
49             1;
50              
51             __END__