blib/lib/WordListRole/RandomSeekPick.pm

filter: Statementstmt 39.5% 17 / 43 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 30.5% 22 / 72 CC 11 SCAR 44.1
CC
11
Cov
27%
pick
111.5
BEGIN
1.0
BEGIN
1.0
CRAP 82.0
↑ Summary
1 package WordListRole::RandomSeekPick;
2  
3 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 1 our $DATE = '2020-05-23'; # DATE
5 1 our $DIST = 'WordListRole-RandomSeekPick'; # DIST
6 1 our $VERSION = '0.004'; # VERSION
7  
8 1 ▶use strict 'subs', 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub pick {
Subroutine pick: not called CC=11 SCAR=47.1
Pod documented
13 # why is still this necesary? does Role::Tiny enforce strict?
14 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 my ($self, $n, $allow_duplicates) = @_;
17  
18 0 ▶ $n = 1 if !defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
19 0 ▶ die "Please don't pick too many items" if $n >= 10_000;
Branch: 1 branch
BranchTrueFalseTotal
if $n >= 10000 0 0 0
20  
21 0 ▶ my $class = $self->{orig_class} || ref($self);
Condition0%0/3
$self->{'orig_class'} || ref $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'orig_class'} || ref $self
$self->{'orig_class'} ref $self
Truth table0%0/3
A || B
A= $self->{'orig_class'}B= ref $self
ABresult
1X1
011
000
22 0 my $fh = \*{"$class\::DATA"};
23 0 my $start = ${"$class\::DATA_POS"};
24 0 my $end = -s $fh;
25  
26 0 my (%items, @items);
27 0 my $iter = 0;
28 0 while (1) {
29 0 ▶ if ($allow_duplicates) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_duplicates) { } 0 0 0
30 0 ▶ last if @items >= $n;
Branch: 1 branch
BranchTrueFalseTotal
if @items >= $n 0 0 0
31 } else {
32 0 ▶ last if keys(%items) >= $n;
Branch: 1 branch
BranchTrueFalseTotal
if keys %items >= $n 0 0 0
33 }
34  
35 0 seek $fh, $start + int(rand() * ($end-$start+1)), 0;
36 0 <$fh>; # skip the line fragment
37 0 ▶ seek $fh,0,0 if eof $fh; # wrap if hit EOF
Branch: 1 branch
BranchTrueFalseTotal
if eof $fh 0 0 0
38 0 chomp(my $item = scalar <$fh>); # get the next line
39  
40 0 ▶ if ($allow_duplicates) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_duplicates) { } 0 0 0
41 0 push @items, $item;
42 } else {
43 0 $items{$item}++;
44 0 ▶ last if $iter++ > 50_000;
Branch: 1 branch
BranchTrueFalseTotal
if $iter++ > 50000 0 0 0
45 }
46 }
47  
48 0 ▶ if ($allow_duplicates) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_duplicates) { } 0 0 0
49 0 return @items;
50 } else {
51 0 return keys %items;
52 }
53 }
54  
55 1 1;
56 # ABSTRACT: Provide a pick() implementation that random-seeks DATA
57  
58 __END__
↑ Summary