File Coverage

blib/lib/Crypt/Random/Provider/File.pm
Criterion Covered Total %
statement 37 40 92.5
branch 5 10 50.0
condition 4 11 36.3
subroutine 8 9 88.8
pod 0 3 0.0
total 54 73 73.9


line stmt bran cond sub pod time code
1 4     4   31 use strict;
  4         9  
  4         200  
2 4     4   22 use warnings;
  4         9  
  4         319  
3             package Crypt::Random::Provider::File;
4 4     4   25 use Carp;
  4         8  
  4         435  
5 4     4   27 use Math::Pari qw(pari2num);
  4         6  
  4         38  
6 4     4   612 use Fcntl;
  4         15  
  4         3403  
7              
8             our $VERSION = '1.57';
9              
10             sub _defaultsource {
11 0     0   0 return;
12             }
13              
14              
15             sub new {
16              
17 1701     1701 0 536314 my ($class, %args) = @_;
18 1701   33     15959 my $self = { Source => $args{File} || $args{Device} || $args{Filename} || $class->_defaultsource() };
19 1701         18584 return bless $self, $class;
20              
21             }
22              
23              
24             sub get_data {
25              
26 1701     1701 0 9961 my ($self, %params) = @_;
27 1701 50       4238 $self = {} unless ref $self;
28              
29 1701         4634 my $size = $params{Size};
30 1701   50     8311 my $skip = $params{Skip} || $$self{Skip} || '';
31 1701         4089 my $q_skip = quotemeta($skip);
32              
33 1701 50 33     6534 if ($size && ref $size eq "Math::Pari") {
34 0         0 $size = pari2num($size);
35             }
36              
37 1701   33     7935 my $bytes = $params{Length} || (int($size / 8) + 1);
38              
39 1701         78376 sysopen RANDOM, $$self{Source}, O_RDONLY;
40              
41 1701         7074 my($r, $read, $rt) = ('', 0);
42 1701         13839 while ($read < $bytes) {
43 1701         10867 my $howmany = sysread RANDOM, $rt, $bytes - $read;
44 1701 50       3815 next unless $howmany;
45 1701 50       3874 if ($howmany == -1) {
46 0         0 croak "Error while reading from $$self{Source}. $!"
47             }
48 1701 50       3662 $rt =~ s/[$q_skip]//g if $skip;
49 1701         3113 $r .= $rt;
50 1701         4424 $read = length $r;
51             }
52              
53 1701         8461 $r;
54              
55             }
56              
57              
58             sub available {
59 704     704 0 1897 my ($class) = @_;
60 704         2446 return -e $class->_defaultsource();
61             }
62              
63              
64             1;
65