blib/lib/Filename/Executable.pm

filter: Statementstmt 85.2% 29 / 34 Branchbran 33.3% 2 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 73.4% 36 / 49 SCAR 14.6
CC
4
Cov
74%
check_executable_filename
20.0
BEGIN
1.0
BEGIN
1.0
CRAP 4.3
↑ Summary
1 package Filename::Executable;
2  
3 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 1 our $DATE = '2020-10-02'; # DATE
5 1 our $DIST = 'Filename-Executable'; # DIST
6 1 our $VERSION = '0.001'; # VERSION
7  
8 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 our @EXPORT_OK = qw(check_executable_filename);
14  
15 1 our %TYPES = (
16 'perl script' => [qw/.pl/],
17 'php script' => [qw/.php/],
18 'python script' => [qw/.py/],
19 'ruby script' => [qw/.rb/],
20 'shell script' => [qw/.sh .bash/],
21 'shell archive' => [qw/.shar/],
22 'dos program' => [qw/.exe .com .bat/],
23 'appimage' => [qw/.appimage/],
24 );
25 1 our %EXTS = map { my $type = $_; map {($_=> $type)} @{ $TYPES{$type} } } keys %TYPES;
26 1 ▶our $RE_STR = join("|", sort {length($b) <=> length($a) || $a cmp $b} keys %EXTS);
Branch: 1 branch
BranchTrueFalseTotal
length $b <=> length $a or $a cmp $b 9 18 27
27 1 our $RE_NOCI = qr/\A(.+)($RE_STR)\z/;
28 1 our $RE_CI = qr/\A(.+)($RE_STR)\z/i;
29  
30 1 our %SPEC;
31  
32 $SPEC{check_executable_filename} = {
33 1 v => 1.1,
34 summary => 'Check whether filename indicates being an executable program/script',
35 description => <<'_',
36  
37  
38 _
39 args => {
40 filename => {
41 schema => 'str*',
42 req => 1,
43 pos => 0,
44 },
45 # XXX recurse?
46 ci => {
47 summary => 'Whether to match case-insensitively',
48 schema => 'bool',
49 default => 1,
50 },
51 },
52 result_naked => 1,
53 result => {
54 schema => ['any*', of=>['bool*', 'hash*']],
55 description => <<'_',
56  
57 Return false if no archive suffixes detected. Otherwise return a hash of
58 information, which contains these keys: `exec_type`, `exec_ext`,
59 `exec_name`.
60  
61 _
62 },
63 examples => [
64 {
65 args => {filename => 'foo.pm'},
66 naked_result => 0,
67 },
68 {
69 args => {filename => 'foo.appimage'},
70 naked_result => {exec_name=>'foo', exec_type=>'appimage', exec_ext=>'.appimage'},
71 },
72 {
73 summary => 'Case-insensitive by default',
74 args => {filename => 'foo.Appimage'},
75 naked_result => {exec_name=>'foo', exec_type=>'appimage', exec_ext=>'.Appimage'},
76 },
77 {
78 summary => 'Case-sensitive',
79 args => {filename => 'foo.Appimage', ci=>0},
80 naked_result => 0,
81 },
82 ],
83 };
84 sub check_executable_filename {
85 0 ▶ my %args = @_;
Subroutine check_executable_filename: not called CC=4 SCAR=30.0
Pod documented
86  
87 0 my $filename = $args{filename};
88 0 ▶ my $ci = $args{ci} // 1;
Condition0%0/2
$args{"ci"} // 1
l!l
00
MC/DC0%0/1
$args{"ci"} // 1
$args{"ci"}
Truth table0%0/2
A
A= $args{"ci"}
Aresult
11
00
89  
90 #use DD; dd \%EXTS;
91 0 ▶ my ($name, $ext) = $filename =~ ($ci ? $RE_CI : $RE_NOCI)
Branch: 2 branches
BranchTrueFalseTotal
() = $filename =~ ($ci ? $Filename::Executable::RE_CI : $Filename::Executable::RE_NOCI) or return 0 0 0 0
$ci ? : 0 0 0
92 or return 0;
93 return {
94 exec_name => $name,
95 exec_ext => $ext,
96 0 exec_type => $EXTS{lc $ext},
97 };
98 }
99  
100 1 1;
101 # ABSTRACT: Check whether filename indicates being an executable program/script
102  
103 __END__
↑ Summary