File Coverage

blib/lib/App/whichpm.pm
Criterion Covered Total %
statement 57 63 90.4
branch 10 10 100.0
condition n/a
subroutine 15 15 100.0
pod 1 1 100.0
total 83 89 93.2


line stmt bran cond sub pod time code
1             package App::whichpm;
2              
3             =head1 NAME
4              
5             App::whichpm - locate a Perl module and it's version
6              
7             =head1 SYNOPSIS
8              
9             use App::whichpm 'which_pm';
10             my ($filename, $version) = which_pm('App::whichpm');
11             my $filename = App::whichpm::find('App::whichpm');
12              
13             from shell:
14              
15             whichpm App::whichpm
16             whichpm Universe::ObservableUniverse::Filament::SuperCluster::Cluster::Group::Galaxy::Arm::Bubble::InterstellarCloud::SolarSystem::Earth
17              
18             =head1 DESCRIPTION
19              
20             Loads a given module and reports it's location and version.
21              
22             The similar function can be achieved via:
23              
24             perldoc -l Some::Module
25             perl -MSome::Module -le 'print $INC{"Some/Module.pm"}'
26             perl -MSome::Module -le 'print Some::Module->VERSION'
27             pmpath Some::Module
28             pmvers Some::Module
29              
30             =cut
31              
32 1     1   19498 use warnings;
  1         3  
  1         37  
33 1     1   3 use strict;
  1         2  
  1         33  
34              
35             our $VERSION = '0.05';
36              
37 1     1   4 use File::Spec;
  1         4  
  1         14  
38              
39 1     1   4 use base 'Exporter';
  1         1  
  1         385  
40             our @EXPORT_OK = qw(
41             which_pm
42             );
43              
44             =head1 EXPORTS
45              
46             =head2 which_pm
47              
48             same as L only exported under C name.
49              
50             =cut
51              
52             *which_pm = *find;
53              
54             =head1 FUNCTIONS
55              
56             =head2 find($module_name)
57              
58             Loads the C<$module_name>.
59              
60             In scalar context returns filename corresponding to C<$module_name>.
61             In array context returns filename and version.
62              
63             C<$module_name> can be either C or C
64              
65             =cut
66              
67             sub find {
68 10     10 1 98 my $module_name = shift;
69 10         9 my $module_filename;
70              
71 10 100       35 if ($module_name =~ m/\.pm$/xms) {
72 2         5 $module_name = substr($module_name, 0, -3);
73 2         14 $module_name =~ s{[/\\]}{::}g;
74             }
75              
76 10         12 $module_filename = $module_name.'.pm';
77 10         31 my $module_inc_filename = join('/', split('::', $module_filename));
78 10         84 $module_filename = File::Spec->catfile(split('::', $module_filename));
79              
80 1     1   311 eval "use $module_name;";
  1     1   25  
  1     1   13  
  1     1   8  
  1     1   1  
  1     1   20  
  1     1   323  
  1     1   30  
  1     1   16  
  1     1   4  
  1         2  
  1         14  
  1         257  
  0         0  
  0         0  
  1         138  
  0         0  
  0         0  
  1         4  
  1         2  
  1         11  
  1         334  
  0         0  
  0         0  
  1         5  
  1         1  
  1         12  
  1         9  
  1         9  
  1         16  
  10         599  
81 10         57 my $filename = $INC{$module_inc_filename};
82              
83             # if the filename is not in %INC then try to search the @INC folders
84 10 100       23 if (not $filename) {
85 3         8 foreach my $inc_path (@INC) {
86 21         133 my $module_full_filename = File::Spec->catfile($inc_path, $module_filename);
87 21 100       257 return $module_full_filename
88             if -f $module_full_filename;
89             }
90 2         13 return;
91             }
92              
93             # MSWin32 has unix / in the %INC folder paths, so recreate the filename
94 7         102 $filename = File::Spec->catfile(split(m{[/\\]}, $filename));
95              
96 7 100       25 if (wantarray) {
97 3         5 my $version = eval { $module_name->VERSION };
  3         30  
98 3 100       30 return ($filename, (defined $version ? $version : ()));
99             }
100              
101 4         20 return $filename
102             }
103              
104             1;
105              
106              
107             __END__