| 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 | Locates a given module and reports its path and version without loading it. | ||||||||||
| 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 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 6 | our $VERSION = '0.07'; | |||||||||
| 36 | |||||||||||
| 37 | 6 | ▶ | use Module::Metadata; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 6 | ▶ | use Module::Path qw(module_path); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | |||||||||||
| 40 | 6 | ▶ | use base 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 6 | our @EXPORT_OK = qw( | |||||||||
| 42 | which_pm | ||||||||||
| 43 | ); | ||||||||||
| 44 | |||||||||||
| 45 | =head1 EXPORTS | ||||||||||
| 46 | |||||||||||
| 47 | =head2 which_pm | ||||||||||
| 48 | |||||||||||
| 49 | same as L</find> only exported under C<which_pm> name. | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | 6 | *which_pm = *find; | |||||||||
| 54 | |||||||||||
| 55 | =head1 FUNCTIONS | ||||||||||
| 56 | |||||||||||
| 57 | =head2 find($module_name) | ||||||||||
| 58 | |||||||||||
| 59 | Locates C<$module_name> without loading it. | ||||||||||
| 60 | |||||||||||
| 61 | In scalar context returns filename corresponding to C<$module_name>. | ||||||||||
| 62 | In array context returns filename and version. | ||||||||||
| 63 | |||||||||||
| 64 | C<$module_name> can be either C<Some::Module::Name> or C<Some/Module/Name.pm> | ||||||||||
| 65 | |||||||||||
| 66 | =cut | ||||||||||
| 67 | |||||||||||
| 68 | 13 | ▶ | sub find { | ||||||||
|
Subroutine
find: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 69 | 13 | my $module_name = shift; | |||||||||
| 70 | |||||||||||
| 71 | 13 | ▶ | if ($module_name =~ m/\.pm$/xms) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 2 | $module_name = substr($module_name, 0, -3); | |||||||||
| 73 | 2 | $module_name =~ s{[/\\]}{::}g; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 13 | my $filename = module_path($module_name); | |||||||||
| 77 | 13 | ▶ | return if not defined $filename; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | |||||||||||
| 79 | 10 | ▶ | if (wantarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 6 | my $metadata = Module::Metadata->new_from_file($filename); | |||||||||
| 81 | 6 | my $version = $metadata->version($module_name); | |||||||||
| 82 | 6 | ▶ | return ($filename, (defined $version ? $version : ())); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 4 | return $filename | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 6 | 1; | |||||||||
| 89 | |||||||||||
| 90 | |||||||||||
| 91 | __END__ | ||||||||||