blib/lib/File/Random.pm

filter: Statementstmt 100% 92 / 92 Branchbran 96.1% 50 / 52 Conditioncond 85.4% 41 / 48 MC/DCmcdc 65.3% 17 / 26 Subroutinesub 100% 17 / 17 Podpod 100% 3 / 3 totaltotal 92.4% 220 / 238 SCAR 38.8
CC
48
Cov
95%
random_line
19.0
_params_random_file
13.2
_valid_file
5.9
CRAP 48.2
↑ Summary test/lib/ContentOfRandomFileTestOptions.pm »
1 package File::Random;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Want qw/howmany/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 require Exporter;
12  
13 1 our @ISA = qw(Exporter);
14  
15 1 our %EXPORT_TAGS = (
16 ':all' => [ qw(
17 random_file
18 content_of_random_file corf
19 random_line
20 ) ],
21 # and for some backward compability
22 'all' => [ qw(
23 random_file
24 content_of_random_file corf
25 random_line
26 ) ]
27 );
28  
29 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{':all'} }, 'corf' );
30  
31 1 our @EXPORT = qw(
32
33 );
34 1 our $VERSION = '0.20';
35  
36 sub _standard_dir($);
37 sub _dir(%);
38  
39 sub random_file {
40 36620 ▶ my @params = my ($dir, $check, $recursive, $follow) = _params_random_file(@_);
Subroutine random_file: called CC=2 SCAR=6.9
Pod documented
41
42 36566 ▶ return $recursive ? _random_file_recursive (@params)
Branch: 1 branch
BranchTrueFalseTotal
$recursive ? : 12250 24316 36566
43 : _random_file_non_recursive(@params);
44 }
45  
46 1 *corf = *content_of_random_file;
47  
48 sub content_of_random_file {
49 18560 ▶ my %args = @_;
Subroutine content_of_random_file: called CC=4 SCAR=13.9
Pod documented
50 18560 ▶ my $rf = random_file(%args) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rf = random_file(%args) or return undef 2274 16250 18524
51 16250 my $dir = _standard_dir _dir %args;
52
53 16250 ▶ open RANDOM_FILE, "<", "$dir/$rf"
Branch: 1 branch
BranchTrueFalseTotal
open RANDOM_FILE, "<", "$dir/$rf" or die "Can't open the randomly selected file '$dir/${rf}'" 0 16250 16250
54 or die "Can't open the randomly selected file '$dir/$rf'";
55 16250 my @content = (<RANDOM_FILE>);
56 16250 close RANDOM_FILE;
57 16250 ▶ return wantarray ? @content : join "", @content;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 15750 500 16250
58 }
59  
60 sub random_line {
61 345 ▶ my ($fname, $nr_of_lines) = @_;
Subroutine random_line: called CC=19 SCAR=29.4
Pod documented
62 345 ▶ defined $fname or die "Need a defined filename to read a random line";
Branch: 1 branch
BranchTrueFalseTotal
defined $fname or die "Need a defined filename to read a random line" 1 344 345
63 344 ▶ if (!defined($nr_of_lines) and wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $nr_of_lines and wantarray) 68 276 344
Condition100%3/3
not defined $nr_of_lines and wantarray
!ll&&!rl&&r
7320368
MC/DC100%2/2
not defined $nr_of_lines and wantarray
not defined $nr_of_lines wantarray
Truth table100%3/3
not(A) and B
A= not defined $nr_of_linesB= wantarray
ABresult
0X0
100
111
64 68 ▶ $nr_of_lines = howmany() || 1;
Condition100%2/2
&howmany() || 1
l!l
671
MC/DC100%1/1
&howmany() || 1
&howmany()
Truth table100%2/2
A
A= &howmany()
Aresult
11
00
65 }
66 344 ▶ unless (!defined($nr_of_lines) or $nr_of_lines =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $nr_of_lines or $nr_of_lines =~ /^\d+$/) 2 342 344
Condition100%3/3
not defined $nr_of_lines or $nr_of_lines =~ /^\d+$/
l!l&&r!l&&!r
2031392
MC/DC100%2/2
not defined $nr_of_lines or $nr_of_lines =~ /^\d+$/
not defined $nr_of_lines $nr_of_lines =~ /^\d+$/
Truth table100%3/3
not(A) or B
A= not defined $nr_of_linesB= $nr_of_lines =~ /^\d+$/
ABresult
1X1
011
000
67 2 die "Number of random_lines should be a number, not '$nr_of_lines'";
68 }
69 342 ▶ defined($nr_of_lines) and $nr_of_lines == 0 and
Branch: 1 branch
BranchTrueFalseTotal
defined $nr_of_lines and $nr_of_lines == 0 and carp(q[doesn't make a lot of sense to return 0 random lines, ] . "you called me with random_line($fname,$nr_of_lines)") 1 341 342
Condition100%3/3
defined $nr_of_lines and $nr_of_lines == 0
!ll&&!rl&&r
2031381
MC/DC100%2/2
defined $nr_of_lines and $nr_of_lines == 0
defined $nr_of_lines $nr_of_lines == 0
Truth table100%3/3
A and B
A= defined $nr_of_linesB= $nr_of_lines == 0
ABresult
0X0
100
111
70 carp "doesn't make a lot of sense to return 0 random lines, " .
71 "you called me with random_line($fname,$nr_of_lines)";
72 342 ▶ $nr_of_lines ||= 1;
Condition100%2/2
$nr_of_lines ||= 1
l!l
138204
MC/DC100%1/1
$nr_of_lines ||= 1
$nr_of_lines
Truth table100%2/2
A
A= $nr_of_lines
Aresult
11
00
73 342 my @line = ();
74 342 ▶ open FILE, '<', $fname or die "Can't open '$fname' to read random_line";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<", $fname or die "Can't open '${fname}' to read random_line" 2 340 342
75 340 ▶ if ($nr_of_lines == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nr_of_lines == 1) { } 203 137 340
76 # Algorithm from Cookbook, chapter 8.6
77 203 ▶ rand($.) < 1 && ($line[0] = $_) while (<FILE>);
Branch: 2 branches
BranchTrueFalseTotal
defined($_ = readline FILE) and do { rand $. < 1 and $line[0] = $_ } 1616 203 1819
rand $. < 1 and $line[0] = $_ 542 1074 1616
78 } else {
79 wantarray or
80 137 ▶ carp "random_line($fname,$nr_of_lines) was called in scalar context, ".
Branch: 1 branch
BranchTrueFalseTotal
wantarray or carp("random_line($fname,$nr_of_lines) was called in scalar context, " . q[what doesn't make a lot sense]) 1 136 137
81 "what doesn't make a lot sense";
82 137 ▶ while (<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 1096 137 1233
83 1096 for my $lnr (0 .. $nr_of_lines-1) {
84 5640 ▶ $line[$lnr] = $_ if rand($.) < 1;
Branch: 1 branch
BranchTrueFalseTotal
if rand $. < 1 1940 3700 5640
85 }
86 }
87 }
88 340 close FILE;
89 340 ▶ return wantarray ? @line : $line[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 137 203 340
90 }
91  
92 sub _random_file_non_recursive {
93 24316 ▶ my ($dir, $check) = @_;
Subroutine _random_file_non_recursive: called CC=4 SCAR=13.9
94  
95 24316 ▶ opendir DIR, $dir or die "Can't open directory '$dir'";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or die "Can't open directory '${dir}'" 18 24298 24316
96 24298 ▶ my @files = grep {-f "$dir/$_" and _valid_file($check, $_)} (readdir DIR);
Branch: 1 branch
BranchTrueFalseTotal
-f "$dir/$_" and _valid_file($check, $_) 128000 88192 216192
97 24298 closedir DIR;
98  
99 24298 ▶ return undef unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 4548 19750 24298
100 19750 return $files[rand @files];
101 }
102  
103 sub _random_file_recursive {
104 12250 ▶ my ($dir, $check, $recursive, $follow) = @_;
Subroutine _random_file_recursive: called CC=1 SCAR=0.0
105  
106 12250 my $i = 1;
107 12250 my $fname;
108  
109 my $accept_routine = sub {
110 196000 ▶ return unless -f;
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ 49000 147000 196000
111
112 # Calculate filename with relative path
113 147000 my ($f) = $File::Find::name =~ m:^$dir[/\\]*(.*)$:;
114 147000 ▶ return unless _valid_file($check,$f);
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_file($check, $f) 9000 138000 147000
115 # Algorithm from Cookbook, chapter 8.6
116 # similar to selecting a random line from a file
117 138000 ▶ if (rand($i++) < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand $i++ < 1) 36923 101077 138000
118 36923 $fname = $f;
119 }
120 12250 };
121 12250 find({wanted => $accept_routine, follow => $follow}, $dir);
122  
123 12250 return $fname;
124 }
125  
126 sub _valid_file {
127 275000 ▶ my ($check, $name) = @_;
Subroutine _valid_file: called CC=5 SCAR=17.8
128 275000 for (ref($check)) {
129 275000 ▶ /Regexp/ && return $name =~ /$check/
Branch: 1 branch
BranchTrueFalseTotal
/Regexp/ and return $name =~ /$check/ or /CODE/ and $_ = $name, (return &$check($name)) 254000 0 254000
Condition57%4/7
/Regexp/ && (return $name =~ /$check/) || /CODE/ && ($_ = $name, (return &$check($name)))
l!l&&r!l&&!r
02540000
/Regexp/ and return $name =~ /$check/
!ll
25400021000
/CODE/ and $_ = $name, (return &$check($name))
!ll
0254000
MC/DC0%0/3
/Regexp/ && (return $name =~ /$check/) || /CODE/ && ($_ = $name, (return &$check($name)))
/Regexp/ /CODE/ $_ = $name, (return &$check($name))
Truth table0%0/4
A || B and C
A= /Regexp/B= /CODE/C= $_ = $name, (return &$check($name))
ABCresult
1XX1
0111
00X0
0100
130 or /CODE/ && ($_ = $name, return $check->($name));
131 }
132 }
133  
134 sub _dir (%) {
135 52834 ▶ my %args = @_;
Subroutine _dir: called CC=3 SCAR=11.0
136 52834 ▶ return $args{-d} || $args{-dir} || $args{-directory};
Condition100%6/6
$args{'-d'} || $args{'-dir'} || $args{'-directory'}
l!l&&r!l&&!r
33024160123798
$args{"-d"} or $args{"-dir"}
l!l&&r!l&&!r
160121701219810
MC/DC100%3/3
$args{'-d'} || $args{'-dir'} || $args{'-directory'}
$args{"-d"} $args{"-dir"} $args{'-directory'}
Truth table100%4/4
A or B || C
A= $args{"-d"}B= $args{"-dir"}C= $args{'-directory'}
ABCresult
1XX1
01X1
0011
0000
137 }
138  
139 sub _params_random_file {
140 36620 ▶ my %args = @_;
Subroutine _params_random_file: called CC=13 SCAR=25.8
141
142 36620 for (qw/-d -dir -directory -c -check/) {
143 183040 ▶ exists $args{$_} and ! $args{$_} and
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$_} and not $args{$_} and die "Parameter $_ is declared with a false value" 36 183004 183040
Condition100%3/3
exists $args{$_} and not $args{$_}
!ll&&!rl&&r
1256495735536
MC/DC100%2/2
exists $args{$_} and not $args{$_}
exists $args{$_} not $args{$_}
Truth table100%3/3
A and not(B)
A= exists $args{$_}B= not $args{$_}
ABresult
0X0
100
111
144 die "Parameter $_ is declared with a false value";
145 }
146
147 36584 foreach (keys %args) {
148 75635 ▶ /^\-(d|dir|directory|
Branch: 1 branch
BranchTrueFalseTotal
/^\-(d|dir|directory| c|check| r|rec|recursive| f|follow)$/x or carp("Unknown option '${_}'") 48 75587 75635
149 c|check|
150 r|rec|recursive|
151 f|follow)$/x or carp "Unknown option '$_'";
152 }
153
154 36584 my $dir = _standard_dir _dir %args;
155 36584 ▶ my $check = $args{-c} || $args{-check} || sub {"always O.K."};
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition83%5/6
$args{'-c'} || $args{'-check'} || sub { 'always O.K.'; }
l!l&&r!l&&!r
23801127830
$args{"-c"} or $args{"-check"}
l!l&&r!l&&!r
77671603412783
MC/DC0%0/3
$args{'-c'} || $args{'-check'} || sub { 'always O.K.'; }
$args{"-c"} $args{"-check"} sub { 'always O.K.'; }
Truth table75%3/4
A or B || C
A= $args{"-c"}B= $args{"-check"}C= sub { 'always O.K.'; }
ABCresult
1XX1
01X1
0011
0000
156 36584 ▶ my $recursive = $args{-r} || $args{-rec} || $args{-recursive};
Condition100%6/6
$args{'-r'} || $args{'-rec'} || $args{'-recursive'}
l!l&&r!l&&!r
8000425024334
$args{"-r"} or $args{"-rec"}
l!l&&r!l&&!r
4000400028584
MC/DC100%3/3
$args{'-r'} || $args{'-rec'} || $args{'-recursive'}
$args{"-r"} $args{"-rec"} $args{'-recursive'}
Truth table100%4/4
A or B || C
A= $args{"-r"}B= $args{"-rec"}C= $args{'-recursive'}
ABCresult
1XX1
01X1
0011
0000
157 36584 ▶ my $follow = $args{-f} || $args{-follow};
Condition33%1/3
$args{'-f'} || $args{'-follow'}
l!l&&r!l&&!r
0036584
MC/DC0%0/2
$args{'-f'} || $args{'-follow'}
$args{'-f'} $args{'-follow'}
Truth table33%1/3
A || B
A= $args{'-f'}B= $args{'-follow'}
ABresult
1X1
011
000
158  
159 36584 ▶ unless (!defined($check) or (scalar ref($check) =~ /^(Regexp|CODE)$/)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $check or scalar ref($check) =~ /^(Regexp|CODE)$/) 18 36566 36584
Condition50%1/2
not defined $check or scalar ref($check) =~ /^(Regexp|CODE)$/
l!l
036584
MC/DC0%0/1
not defined $check or scalar ref($check) =~ /^(Regexp|CODE)$/
not defined $check
Truth table50%1/2
not(A)
A= not defined $check
Aresult
11
00
160 18 die "-check Parameter has to be either a Regexp or a sub routine,".
161 "not a '" . ref($check) . "'";
162 }
163
164 36566 return ($dir, $check, $recursive, $follow);
165 }
166  
167 sub _standard_dir($) {
168 52834 ▶ my $dir = shift() || '.';
Subroutine _standard_dir: called CC=2 SCAR=6.9
Condition100%2/2
shift() || '.'
l!l
490363798
MC/DC100%1/1
shift() || '.'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
169 52834 $dir =~ s:[/\\]+$::;
170 52834 return $dir;
171 }
172  
173 1 1;
174 __END__
↑ Summary test/lib/ContentOfRandomFileTestOptions.pm »