| 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
| |||||||||||||||
| 56 | 0 | ▶ | unless ($args{all}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 0 | ▶ | next if $mem{$row->{script}}++; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | } | ||||||||||||||
| 59 | 0 | push @res, $row; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 0 | ▶ | if (@$scripts == 1) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 0 | @res = map { $_->{module} } @res; | |||||||||||||
| 64 | 0 | ▶ | if (!@res) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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__ | ||||||||||||||