blib/lib/File/RandomLine.pm

filter: Statementstmt 100% 55 / 55 Branchbran 100% 28 / 28 Conditioncond 100% 18 / 18 MC/DCmcdc 100% 11 / 11 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 100% 124 / 124 CC 22 SCAR 20.4
CC
22
Cov
100%
next
12.0
new
9.0
_index_file
2.0
CRAP 7.7
↑ Summary
1 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package File::RandomLine;
6 # ABSTRACT: Retrieve random lines from a file
7 3 our $VERSION = '0.20'; # VERSION
8  
9 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Required modules
12 3 ▶use Want 'howmany';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15  
16 7 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
17 7 my ($class, $filename, $args) = @_;
18 7 ▶ croak "new requires a filename parameter" unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 1 6 7
19 6 ▶ my $algo = $args->{algorithm} || q{};
Condition100%2/2
$args->{'algorithm'} || ''
l!l
24
MC/DC100%1/1
$args->{'algorithm'} || ''
$args->{'algorithm'}
Truth table100%2/2
A
A= $args->{'algorithm'}
Aresult
11
00
20 6 ▶ croak "unknown algorithm '$algo'" if $algo && $algo !~ /fast|uniform/i;
Branch: 1 branch
BranchTrueFalseTotal
if $algo and not $algo =~ /fast|uniform/i 1 5 6
Condition100%3/3
$algo and not $algo =~ /fast|uniform/i
!ll&&!rl&&r
411
MC/DC100%2/2
$algo and not $algo =~ /fast|uniform/i
$algo not $algo =~ /fast|uniform/i
Truth table100%3/3
A and not(B)
A= $algoB= not $algo =~ /fast|uniform/i
ABresult
0X0
100
111
21 5 ▶ open(my $fh, "<", $filename) or croak "Can't read $filename";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or croak("Can't read $filename") 3 2 5
22 3 ▶ my $line_index = lc $algo eq 'uniform' ? _index_file($fh) : undef ;
Branch: 1 branch
BranchTrueFalseTotal
lc $algo eq 'uniform' ? : 1 2 3
23 3 my $filesize = -s $fh;
24 3 ▶ my $self = {
Branch: 1 branch
BranchTrueFalseTotal
$line_index ? : 1 2 3
25 fh => $fh,
26 line_index => $line_index,
27 line_count => $line_index ? scalar @$line_index : undef,
28 filesize => $filesize
29 };
30 3 ▶ return bless( $self, ref($class) ? ref($class) : $class );
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 1 2 3
31 }
32
33 #--------------------------------------------------------------------------#
34 # _index_file
35 #--------------------------------------------------------------------------#
36  
37 1 ▶sub _index_file {
Subroutine _index_file: called CC=2 SCAR=6.9
38 1 my ($fh) = @_;
39 1 my @index;
40 1 ▶ while (! eof $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 4 1 5
41 4 push @index, tell $fh;
42 4 <$fh>;
43 }
44 1 return \@index;
45 }
46  
47 #--------------------------------------------------------------------------#
48 # next()
49 #--------------------------------------------------------------------------#
50  
51  
52 60 ▶sub next {
Subroutine next: called CC=12 SCAR=24.8
Pod documented
53 60 my ($self,$n) = @_;
54 # behavior copied from File::Random
55 60 ▶ if (!defined($n) and wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $n and wantarray) 4 56 60
Condition100%3/3
not defined $n and wantarray
!ll&&!rl&&r
10464
MC/DC100%2/2
not defined $n and wantarray
not defined $n wantarray
Truth table100%3/3
not(A) and B
A= not defined $nB= wantarray
ABresult
0X0
100
111
56 4 $n = howmany();
57 4 ▶ $n ||= 1;
Condition100%2/2
$n ||= 1
l!l
22
MC/DC100%1/1
$n ||= 1
$n
Truth table100%2/2
A
A= $n
Aresult
11
00
58 }
59 60 ▶ unless (!defined($n) or $n =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $n or $n =~ /^\d+$/) 6 54 60
Condition100%3/3
not defined $n or $n =~ /^\d+$/
l!l&&r!l&&!r
4686
MC/DC100%2/2
not defined $n or $n =~ /^\d+$/
not defined $n $n =~ /^\d+$/
Truth table100%3/3
not(A) or B
A= not defined $nB= $n =~ /^\d+$/
ABresult
1X1
011
000
60 6 croak "Number of random_lines should be a positive integer, not '$n'";
61 }
62 54 ▶ carp "Strange call to File::Random->next(): 0 random lines requested"
Branch: 1 branch
BranchTrueFalseTotal
if defined $n and $n == 0 2 52 54
Condition100%3/3
defined $n and $n == 0
!ll&&!rl&&r
4662
MC/DC100%2/2
defined $n and $n == 0
defined $n $n == 0
Truth table100%3/3
A and B
A= defined $nB= $n == 0
ABresult
0X0
100
111
63 if defined($n) and $n == 0;
64 54 ▶ $n ||= 1;
Condition100%2/2
$n ||= 1
l!l
648
MC/DC100%1/1
$n ||= 1
$n
Truth table100%2/2
A
A= $n
Aresult
11
00
65 54 my @sample;
66 54 ▶ while (@sample < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sample < $n) 60 54 114
67 60 ▶ push @sample, $self->{line_index} ? $self->_uniform : $self->_fast;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'line_index'} ? : 12 48 60
68 }
69 54 chomp @sample;
70 54 ▶ return wantarray ? @sample : shift @sample;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 48 54
71 }
72  
73  
74 #--------------------------------------------------------------------------#
75 # Fast Algorithm
76 #--------------------------------------------------------------------------#
77  
78 48 ▶sub _fast {
Subroutine _fast: called CC=2 SCAR=6.9
79 48 my $self = shift;
80 48 my $fh = $self->{fh};
81 48 seek($fh,int(rand($self->{filesize})),0);
82 48 <$fh>; # skip this fragment of a line
83 48 ▶ seek($fh,0,0) if eof $fh; # wrap if hit EOF
Branch: 1 branch
BranchTrueFalseTotal
if eof $fh 12 36 48
84 48 return scalar <$fh>; # get the next line
85 }
86  
87 #--------------------------------------------------------------------------#
88 # Uniform Algorithm
89 #--------------------------------------------------------------------------#
90  
91 12 ▶sub _uniform {
Subroutine _uniform: called CC=1 SCAR=0.0
92 12 my $self = shift;
93 12 my $fh = $self->{fh};
94 12 my $start = $self->{line_index}[int(rand($self->{line_count}))];
95 12 seek($fh,$start,0);
96 12 return scalar <$fh>; # get the next line
97 }
98  
99 3 1;
100  
101 __END__
↑ Summary