File Coverage

lib/Crypt/Random/Provider/File.pm
Criterion Covered Total %
statement 34 37 91.9
branch 6 10 60.0
condition 11 20 55.0
subroutine 7 8 87.5
pod 0 3 0.0
total 58 78 74.4


line stmt bran cond sub pod time code
1             package Crypt::Random::Provider::File;
2 5     5   198 use strict;
  5         196  
  5         169  
3 5     5   74 use Carp;
  5         43  
  5         119  
4 5     5   77 use Math::Pari qw(pari2num);
  5         43  
  5         84  
5 5     5   89 use Fcntl;
  5         47  
  5         101  
6              
7             sub _defaultsource {
8 0     0   0     return;
9             }
10              
11              
12             sub new {
13              
14 1215     1215 0 16610     my ($class, %args) = @_;
15 1215   33     30035     my $self = { Source => $args{File} || $args{Device} || $args{Filename} || $class->_defaultsource() };
      33        
      33        
16 1215         304494     return bless $self, $class;
17              
18             }
19              
20              
21             sub get_data {
22              
23 1215     1215 0 16115     my ($self, %params) = @_;
24 1215 50       14441     $self = {} unless ref $self;
25              
26 1215         24465     my $size = $params{Size};
27 1215   66     23884     my $skip = $params{Skip} || $$self{Skip} || '';
      100        
28 1215         12089     my $q_skip = quotemeta($skip);
29              
30 1215 50 66     16457     if ($size && ref $size eq "Math::Pari") {
31 0         0         $size = pari2num($size);
32                 }
33              
34 1215   66     19548     my $bytes = $params{Length} || (int($size / 8) + 1);
35              
36 1215         16901     sysopen RANDOM, $$self{Source}, O_RDONLY;
37              
38 1215         94141     my($r, $read, $rt) = ('', 0);
39 1215         16664     while ($read < $bytes) {
40 1265         458597         my $howmany = sysread RANDOM, $rt, $bytes - $read;
41 1265 50       214279         next unless $howmany;
42 1265 50       15878         if ($howmany == -1) {
43 0         0             croak "Error while reading from $$self{Source}. $!"
44                     }
45 1265 100       20906         $rt =~ s/[$q_skip]//g if $skip;
46 1265         222217         $r .= $rt;
47 1265         16571         $read = length $r;
48                 }
49              
50 1215         250976     $r;
51              
52             }
53              
54              
55             sub available {
56 1205     1205 0 13655     my ($class) = @_;
57 1205         18141     return -e $class->_defaultsource();
58             }
59              
60              
61             1;
62              
63