File Coverage

blib/lib/App/runscript.pm
Criterion Covered Total %
statement 57 78 73.0
branch 18 26 69.2
condition n/a
subroutine 15 18 83.3
pod n/a
total 90 122 73.7


line stmt bran cond sub pod time code
1 3     3   488943 use strict;
  3         4  
  3         103  
2 3     3   18 use warnings;
  3         9  
  3         196  
3              
4             package App::runscript;
5              
6             # keeping the following $VERSION declaration on a single line is important
7             #<<<
8 3     3   880 use version 0.9915; our $VERSION = version->declare( '1.0.2' );
  3         4151  
  3         21  
9             #>>>
10              
11 3     3   1647 use subs qw( main _croakf _is_dir _locate_install_lib _prepend_install_lib _which );
  3         755  
  3         17  
12 3     3   221 use vars qw( @CARP_NOT );
  3         10  
  3         151  
13              
14 3     3   14 use Config qw( %Config );
  3         5  
  3         103  
15 3     3   14 use File::Basename qw( basename dirname );
  3         5  
  3         266  
16 3     3   20 use File::Spec qw();
  3         16  
  3         85  
17 3     3   1456 use File::Which qw( which );
  3         3999  
  3         185  
18 3     3   1389 use Getopt::Std qw( getopts );
  3         6176  
  3         199  
19 3     3   1570 use POSIX qw( EXIT_SUCCESS );
  3         23340  
  3         19  
20              
21             sub main ( \@ ) {
22 0     0   0 local @ARGV = @{ $_[ 0 ] };
  0         0  
23              
24 0         0 my $opts;
25             {
26 0         0 local $SIG{ __WARN__ } = sub {
27 0     0   0 local @CARP_NOT = qw( Getopt::Std );
28 0         0 my $warning = shift;
29 0         0 chomp $warning;
30 0         0 _croakf $warning;
31 0         0 };
32 0         0 getopts( '-Vh', $opts = {} );
33             }
34 0 0       0 if ( $opts->{ V } ) {
    0          
35 0         0 print STDOUT "runscript $VERSION\n";
36 0         0 return EXIT_SUCCESS;
37             } elsif ( $opts->{ h } ) {
38 0         0 print STDOUT "Usage: runscript [ -V | -h ]\n",
39             " runscript \n";
40 0         0 return EXIT_SUCCESS;
41             }
42              
43             # derive the pathname of the file containing the perl interpreter
44             # https://perldoc.perl.org/perlvar#$%5EX
45 0         0 my $perl_path = $Config{ perlpath };
46 0 0       0 if ( $^O ne 'VMS' ) {
47             $perl_path .= $Config{ _exe }
48 0 0       0 unless $perl_path =~ m/$Config{ _exe }\z/i;
49             }
50              
51 0         0 exec { $perl_path } ( basename( $perl_path ), _prepend_install_lib( @ARGV ) );
  0         0  
52             }
53              
54             sub _croakf ( $@ ) {
55 5     5   358 require Carp;
56 5 100       41 @_ = ( ( @_ == 1 ? shift : sprintf shift, @_ ) . ', stopped' );
57 5         763 goto &Carp::croak;
58             }
59              
60             sub _is_dir ( $ ) {
61 0     0   0 return -d $_[ 0 ];
62             }
63              
64             sub _locate_install_lib ( $ ) {
65 3     3   13 my ( $application ) = @_;
66              
67 3         173 my $install_bin = dirname $application;
68 3 100       74 _croakf "Basename of '%s' is not 'bin'", $install_bin unless basename( $install_bin ) eq 'bin';
69              
70 2         69 my $install_base = dirname $install_bin;
71 2         18 my $install_lib = File::Spec->catdir( $install_base, qw( lib perl5 ) );
72              
73 2 100       8 _croakf "Library path '%s' derived from application '%s' does not exist", $install_lib, $application
74             unless _is_dir $install_lib;
75              
76 1         428 return $install_lib;
77             }
78              
79             sub _prepend_install_lib ( @ ) {
80 5     5   158716 my ( $application ) = @_;
81              
82 5 100       35 if ( File::Spec->file_name_is_absolute( $application ) ) {
83 2 100       55 _croakf "Script '%s' has no execute permission", $application unless -x $application;
84             } else {
85 3         9 $application = _which $application, 1;
86 3 100       1373 _croakf "Cannot find application '%s' in PATH (%s)", $_[ 0 ], $ENV{ PATH } unless defined $application;
87             }
88 3         7 shift;
89              
90 3         9 return ( '-I' . _locate_install_lib( $application ), $application, @_ );
91             }
92              
93             sub _which ( $;$ ) {
94 6     6   252774 my ( $executable, $abs_path ) = @_;
95              
96 6 100       26 _croakf 'Cannot locate undefined executable file' unless defined $executable;
97              
98 5 100       23 return unless my $file = which $executable;
99 3 100       572 if ( $abs_path ) {
100 2         16 require Cwd;
101 2         96 return Cwd::abs_path( $file );
102             } else {
103 1         83 return $file;
104             }
105             }
106              
107             1;