Data/Unixish/pick.pm

filter: Statementstmt 67.7% 21 / 31 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 83.3% 5 / 6 Podpod 100% 1 / 1 totaltotal 57.4% 27 / 47 CC 6 SCAR 31.5
CC
6
Cov
54%
pick
42.0
BEGIN
1.0
BEGIN
1.0
CRAP 23.4
« Data/Unixish/bool.pm ↑ Summary Test/Data/Unixish.pm »
1 package Data::Unixish::pick;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use syntax 'each_on_array'; # to support perl < 5.12
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 #use Log::Any '$log';
8  
9 1 ▶use Data::Unixish::Util qw(%common_args);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 1 our $DATE = '2025-02-24'; # DATE
13 1 our $DIST = 'Data-Unixish'; # DIST
14 1 our $VERSION = '1.574'; # VERSION
15  
16 1 our %SPEC;
17  
18 $SPEC{pick} = {
19 1 v => 1.1,
20 summary => 'Pick one or more random items',
21 args => {
22 %common_args,
23 items => {
24 summary => 'Number of items to pick',
25 schema=>['int*' => {default=>1}],
26 tags => ['main'],
27 cmdline_aliases => { n=>{} },
28 pos => 0,
29 },
30 },
31 tags => [qw/filtering/],
32 };
33 0 ▶sub pick {
Subroutine pick: not called CC=6 SCAR=37.4
Pod documented
34 0 my %args = @_;
35 0 my ($in, $out) = ($args{in}, $args{out});
36 0 ▶ my $n = $args{items} // 1;
Condition0%0/2
$args{"items"} // 1
l!l
00
MC/DC0%0/1
$args{"items"} // 1
$args{"items"}
Truth table0%0/2
A
A= $args{"items"}
Aresult
11
00
37  
38 0 my @picked;
39 0 ▶ while (my ($index, $item) = each @$in) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @$in) 0 0 0
40 0 ▶ if (@picked < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (@picked < $n) { } 0 0 0
41 # we haven't reached n items, put item to picked list, in a random
42 # position
43 0 splice @picked, rand(@picked+1), 0, $item;
44 } else {
45 # we have reached n items, just replace an item randomly, using
46 # algorithm from Learning Perl, slightly modified.
47 0 ▶ rand($index+1) < @picked and
Branch: 1 branch
BranchTrueFalseTotal
rand $index + 1 < @picked and splice @picked, rand @picked, 1, $item 0 0 0
48 splice @picked, rand(@picked), 1, $item;
49 }
50 }
51  
52 0 push @$out, $_ for @picked;
53 0 [200, "OK"];
54 }
55  
56 1 1;
57 # ABSTRACT: Pick one or more random items
58  
59 __END__
« Data/Unixish/bool.pm ↑ Summary Test/Data/Unixish.pm »