| 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
| |||||||||||||||||||||||||||||
| 19 | 6 | ▶ | my $algo = $args->{algorithm} || q{}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'algorithm'} || ''
MC/DC100%1/1
$args->{'algorithm'} || ''
$args->{'algorithm'}
Truth table100%2/2
A
A= $args->{'algorithm'}
| |||||||||||||||||||||||||||||
| 20 | 6 | ▶ | croak "unknown algorithm '$algo'" if $algo && $algo !~ /fast|uniform/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$algo and not $algo =~ /fast|uniform/i
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
| |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | open(my $fh, "<", $filename) or croak "Can't read $filename"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | my $line_index = lc $algo eq 'uniform' ? _index_file($fh) : undef ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 3 | my $filesize = -s $fh; | |||||||||||||||||||||||||||
| 24 | 3 | ▶ | my $self = { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $n and wantarray
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
| |||||||||||||||||||||||||||||
| 56 | 4 | $n = howmany(); | |||||||||||||||||||||||||||
| 57 | 4 | ▶ | $n ||= 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$n ||= 1
MC/DC100%1/1
$n ||= 1
$n
Truth table100%2/2
A
A= $n
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 60 | ▶ | unless (!defined($n) or $n =~ /^\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $n or $n =~ /^\d+$/
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+$/
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $n and $n == 0
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
| |||||||||||||||||||||||||||||
| 63 | if defined($n) and $n == 0; | ||||||||||||||||||||||||||||
| 64 | 54 | ▶ | $n ||= 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$n ||= 1
MC/DC100%1/1
$n ||= 1
$n
Truth table100%2/2
A
A= $n
| |||||||||||||||||||||||||||||
| 65 | 54 | my @sample; | |||||||||||||||||||||||||||
| 66 | 54 | ▶ | while (@sample < $n) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 60 | ▶ | push @sample, $self->{line_index} ? $self->_uniform : $self->_fast; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 54 | chomp @sample; | |||||||||||||||||||||||||||
| 70 | 54 | ▶ | return wantarray ? @sample : shift @sample; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||