App/lcpan/Cmd/script2mod.pm

filter: Statementstmt 43.2% 16 / 37 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 37.0% 20 / 54 CC 7 SCAR 36.8
CC
7
Cov
33%
handle_cmd
56.0
BEGIN
1.0
BEGIN
1.0
CRAP 39.5
« App/lcpan/Cmd/script2dist.pm ↑ Summary App/lcpan/Cmd/rdeps_from_other_author.pm »
1 package App::lcpan::Cmd::script2mod;
2  
3 1 ▶use 5.010001;
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 require App::lcpan;
8  
9 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 1 our $DATE = '2023-09-26'; # DATE
11 1 our $DIST = 'App-lcpan'; # DIST
12 1 our $VERSION = '1.074'; # VERSION
13  
14 1 our %SPEC;
15  
16 1 $SPEC{'handle_cmd'} = {
17 v => 1.1,
18 summary => 'Get module(s) of script(s)',
19 description => <<'_',
20  
21 This returns a module name from the same dist as the script, so one can do
22 something like this (install dist which contains a specified script from CPAN):
23  
24 % cpanm -n `lcpan script2mod pmdir`
25  
26 _
27 args => {
28 %App::lcpan::common_args,
29 %App::lcpan::scripts_args,
30 %App::lcpan::all_args,
31 },
32 };
33 0 ▶sub handle_cmd {
Subroutine handle_cmd: not called CC=7 SCAR=40.3
Pod documented
34 0 my %args = @_;
35  
36 0 my $state = App::lcpan::_init(\%args, 'ro');
37 0 my $dbh = $state->{dbh};
38  
39 0 my $scripts = $args{scripts};
40  
41 0 my $scripts_s = join(",", map {$dbh->quote($_)} @$scripts);
42  
43 0 my $sth = $dbh->prepare("
44 SELECT
45 script.name script,
46 (SELECT name FROM module WHERE file_id=file.id LIMIT 1) module
47 FROM script
48 LEFT JOIN file ON script.file_id=file.id
49 WHERE script.name IN ($scripts_s)
50 ");
51  
52 0 my @res;
53 my %mem;
54 0 $sth->execute;
55 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
56 0 ▶ unless ($args{all}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"all"}) 0 0 0
57 0 ▶ next if $mem{$row->{script}}++;
Branch: 1 branch
BranchTrueFalseTotal
if $mem{$row->{"script"}}++ 0 0 0
58 }
59 0 push @res, $row;
60 }
61  
62 0 ▶ if (@$scripts == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$scripts == 1) 0 0 0
63 0 @res = map { $_->{module} } @res;
64 0 ▶ if (!@res) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @res) { } 0 0 0
elsif (@res == 1) { } 0 0 0
65 0 return [404, "No such script"];
66 } elsif (@res == 1) {
67 0 return [200, "OK", $res[0]];
68 } else {
69 0 return [200, "OK", \@res];
70 }
71 }
72  
73 0 [200, "OK", \@res, {'table.fields'=>[qw/script module/]}];
74 }
75  
76 1 1;
77 # ABSTRACT: Get module(s) of script(s)
78  
79 __END__
« App/lcpan/Cmd/script2dist.pm ↑ Summary App/lcpan/Cmd/rdeps_from_other_author.pm »