File Coverage

blib/lib/Crypt/URandom/MonkeyPatch.pm
Criterion Covered Total %
statement 27 27 100.0
branch 2 2 100.0
condition 2 2 100.0
subroutine 9 9 100.0
pod 1 1 100.0
total 41 41 100.0


line stmt bran cond sub pod time code
1             package Crypt::URandom::MonkeyPatch;
2              
3             # ABSTRACT: override core rand function to use system random sources
4              
5 2     2   275339 use v5.8.0;
  2         8  
6              
7 2     2   9 use strict;
  2         2  
  2         34  
8 2     2   8 use warnings;
  2         2  
  2         112  
9              
10 2     2   865 use Crypt::URandom qw( urandom );
  2         7075  
  2         165  
11              
12 2     2   15 use constant SIZE => 1 << 31;
  2         3  
  2         125  
13 2     2   10 use constant MASK => SIZE - 1;
  2         4  
  2         146  
14              
15             our $VERSION = 'v0.1.3';
16              
17 2     2   896 use version 0.77; $VERSION = version->declare($VERSION);
  2         3284  
  2         16  
18              
19             BEGIN {
20              
21 2     2   529 *CORE::GLOBAL::rand = \&rand;
22             }
23              
24             sub rand(;$) {
25 4   100 4 1 184551 my $a = shift || 1;
26 4         19 my ($b) = unpack( "N", urandom(4) ) & MASK;
27 4 100       120 if ( $ENV{CRYPT_URANDOM_MONKEYPATCH_DEBUG} ) {
28 1         6 my ( $package, $filename, $line ) = caller;
29 1         65 print STDERR __PACKAGE__ . "::urandom used from ${package} line ${line}\n";
30             }
31 4         27 return $a * $b / SIZE;
32             }
33              
34              
35             1;
36              
37             __END__