File Coverage

script/whichpm
Criterion Covered Total %
statement 38 40 95.0
branch 18 24 75.0
condition n/a
subroutine 6 6 100.0
pod n/a
total 62 70 88.5


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             =head1 NAME
4              
5             whichpm - locate a Perl module and it's version
6              
7             =head1 SYNOPSIS
8              
9             # shows path and version (if vailable) of the Perl module
10             whichpm Some::Module::Name
11             whichpm -v Some::Module::Name
12             whichpm --verbose Some::Module::Name
13              
14             # shows just path to the Perl module
15             whichpm -q Some::Module::Name
16             whichpm --quiet Some::Module::Name
17              
18             # add lib/ into @INC
19             whichpmv -l Some::Module::Name
20             # add mylib into @INC
21             whichpmv -Imylib Some::Module::Name
22              
23             # show version of App::whichpm
24             whichpm --version
25              
26             # show/edit .pm file
27             less `whichpm Some::Module::Name`
28             vim `whichpm Some::Module::Name`
29              
30             =head1 DESCRIPTION
31              
32             Loads the module, prints its file system location and version.
33              
34             When STDOUT is not a TTY, prints just file name by default. (-v can force
35             printing version too)
36              
37             =cut
38              
39              
40 4     4   22421 use strict;
  4         35  
  4         172  
41 4     4   23 use warnings;
  4         5  
  4         338  
42              
43 4     4   2132 use App::whichpm qw(which_pm);
  4         14  
  4         373  
44 4     4   3578 use Getopt::Long qw(:config no_ignore_case);
  4         77230  
  4         30  
45 4     4   3660 use Pod::Usage;
  4         314665  
  4         7210  
46              
47 4         648440 exit main();
48              
49             sub main {
50 4     4   11 my $help;
51 4 50       48 my $quiet = (-t STDOUT ? 0 : 1);
52 4         13 my ($version, $verbose, @into_inc, $add_lib);
53              
54             # allow -Ilib style of include dir options
55 4 50       13 @ARGV = map { $_ =~ m/^-I(.+)$/ ? ('-I', $1) : $_ } @ARGV;
  4         26  
56              
57 4 50       41 GetOptions(
58             'help|h' => \$help,
59             'quiet|q' => \$quiet,
60             'version|V' => \$version,
61             'verbose|v' => \$verbose,
62             'I=s' => \@into_inc,
63             'lib|l' => \$add_lib,
64             ) or pod2usage;
65 4 50       4173 pod2usage if $help;
66              
67 4         10 unshift(@INC, @into_inc);
68 4 50       13 unshift(@INC, 'lib')
69             if $add_lib;
70              
71 4 100       14 $quiet = 0 if $verbose;
72              
73             # show version
74 4 50       24 if ($version) {
75 0         0 print App::whichpm->VERSION, "\n";
76 0         0 return 0;
77             }
78              
79 4 100       40 unless (@ARGV) {
80 1         42 print STDERR "usage: $0 Some::Module::Name\n";
81 1         0 return 1;
82             }
83              
84             # lookup for all modules passed on command line
85 3         23 my $all_ok = 1;
86 3         12 while (my $mn = shift @ARGV) {
87 3         15 my ($filename, $version) = which_pm($mn);
88              
89             # don't show version in quiet mode
90 3 100       13 $version = undef
91             if $quiet;
92              
93 3 100       9 if ($filename) {
94 2 100       18 print $filename, (defined $version ? ' '.$version : ()), "\n";
95             }
96             else {
97 1         5 $all_ok = 0;
98             }
99             }
100              
101 3 100         return ($all_ok ? 0 : 1);
102             }