File Coverage

blib/lib/Module/Filename.pm
Criterion Covered Total %
statement 34 34 100.0
branch 5 8 62.5
condition 1 3 33.3
subroutine 8 8 100.0
pod 4 4 100.0
total 52 57 91.2


line stmt bran cond sub pod time code
1             package Module::Filename;
2 3     3   335960 use strict;
  3         9  
  3         135  
3 3     3   25 use warnings;
  3         6  
  3         219  
4 3     3   23 use base qw{Exporter};
  3         8  
  3         546  
5 3     3   1794 use Path::Class qw{file};
  3         146317  
  3         1045  
6              
7             our $VERSION = '0.03';
8             our @EXPORT_OK = qw(module_filename);
9              
10             =head1 NAME
11              
12             Module::Filename - Provides an object oriented, cross platform interface for getting a module's filename
13              
14             =head1 SYNOPSIS
15              
16             use Module::Filename;
17             my $filename=Module::Filename->new->filename("Test::More"); #isa Path::Class::File
18              
19             use Module::Filename qw{module_filename};
20             my $filename=module_filename("Test::More"); #isa Path::Class::File
21              
22             =head1 DESCRIPTION
23              
24             This module returns the filename as a L object. It does not load any packages as it scans. It simply scans @INC looking for a module of the same name as the package passed.
25              
26             =head1 USAGE
27              
28             use Module::Filename;
29             my $filename=Module::Filename->new->filename("Test::More"); #isa Path::Class::File
30             print "Test::More can be found at $filename\n";
31              
32             =head1 CONSTRUCTOR
33              
34             =head2 new
35              
36             my $mf=Module::Filename->new();
37              
38             =cut
39              
40             sub new {
41 2     2 1 380162 my $this = shift();
42 2   33     18 my $class = ref($this) || $this;
43 2         4 my $self = {};
44 2         5 bless $self, $class;
45 2         11 $self->initialize(@_);
46 2         6 return $self;
47             }
48              
49             =head1 METHODS
50              
51             =head2 initialize
52              
53             You can inherit the filename method in your package.
54              
55             use base qw{Module::Filename};
56             sub initialize{do_something_else()};
57              
58             =cut
59              
60             sub initialize {
61 2     2 1 4 my $self = shift();
62 2         14 %$self=@_;
63             }
64              
65             =head2 filename
66              
67             Returns a L object for the first filename that matches the module in the @INC path array.
68              
69             my $filename=Module::Filename->new->filename("Test::More"); #isa Path::Class::File
70             print "Filename: $filename\n";
71              
72             =cut
73              
74             sub filename {
75 5     5 1 781 my $self=shift;
76 5         20 return module_filename(@_);
77             }
78              
79             =head1 FUNCTIONS
80              
81             =head2 module_filename
82              
83             Returns a L object for the first filename that matches the module in the @INC path array.
84              
85             my $filname=module_filename("Test::More"); #isa Path::Class::File
86             print "Filename: $filename\n";
87              
88             =cut
89              
90              
91             sub module_filename {
92 10 50   10 1 214644 die("Error: Module name required.") unless @_;
93 10         25 my $module=shift;
94 10         68 my $file=file(split("::", $module.".pm"));
95 10         1242 my $return=undef;
96 10         30 foreach my $path (@INC) {
97 68 50       4042 next unless defined $path;
98 68 50       172 next if ref($path);
99 68         209 my $filename=file($path, $file);
100 68 100       8812 if (-f $filename) {
101 8         508 $return=$filename;
102 8         25 last; #return the first match in @INC
103             }
104             }
105 10         246 return $return;
106             }
107              
108             =head1 LIMITATIONS
109              
110             The algorithm does not scan inside module files for provided packages.
111              
112             =head1 BUGS
113              
114             Submit to RT and email author.
115              
116             =head1 AUTHOR
117              
118             Michael R. Davis
119              
120             =head1 COPYRIGHT
121              
122             This program is free software licensed under the...
123              
124             The BSD License
125              
126             The full text of the license can be found in the LICENSE file included with this module.
127              
128             =head1 SEE ALSO
129              
130             Module::Filename predates L by almost 4 years but it appears more people prefer L over Module::Filename as it does not have the dependency on L. After the reviews on L. I added the functional API to Module::Filename. So, your decision is simply an object/non-object decision. The operations with the file system that both packages perform outweigh the performance of the object creation in Module::Filename. So, any performance penalty should not be measurable. Since Module::Filename does not need three extra file test operations that Module::Path 0.18 performs on each @INC directory, Module::Filename may actually be faster than L for most applications.
131              
132             =head2 Similar Capabilities
133              
134             L, L %INC, L, L constructor=>new_from_module, method=>file, L property=>"dir", L method=>locate, L method=>find_installed, L
135              
136             =head2 Comparison
137              
138             CPAN modules for getting a module's path L
139              
140             =cut
141              
142             1;