1Module::Path(3) User Contributed Perl Documentation Module::Path(3)
2
3
4
6 Module::Path - get the full path to a locally installed module
7
9 use Module::Path 'module_path';
10
11 $path = module_path('Test::More');
12 if (defined($path)) {
13 print "Test::More found at $path\n";
14 } else {
15 print "Danger Will Robinson!\n";
16 }
17
19 This module provides a single function, "module_path()", which takes a
20 module name and finds the first directory in your @INC path where the
21 module is installed locally. It returns the full path to that file,
22 resolving any symlinks. It is portable and only depends on core
23 modules.
24
25 It works by looking in all the directories in @INC for an appropriately
26 named file:
27
28 • Foo::Bar becomes "Foo/Bar.pm", using the correct directory path
29 separator for your operating system.
30
31 • Iterate over @INC, ignoring any references (see "require" in
32 "perlfunc" if you're surprised to hear that you might find
33 references in @INC).
34
35 • For each directory in @INC, append the partial path ("Foo/Bar.pm"),
36 again using the correct directory path separator. If the resulting
37 file exists, return this path.
38
39 • If a directory in @INC is a symlink, then we resolve the path, and
40 return a path containing the linked-to directory.
41
42 • If no file was found, return "undef".
43
44 I wrote this module because I couldn't find an alternative which dealt
45 with the points listed above, and didn't pull in what seemed like too
46 many dependencies to me.
47
48 The distribution for "Module::Path" includes the "mpath" script, which
49 lets you get the path for a module from the command-line:
50
51 % mpath Module::Path
52
53 The "module_path()" function will also cope if the module name includes
54 ".pm"; this means you can pass a partial path, such as used as the keys
55 in %INC:
56
57 module_path('Test/More.pm') eq $INC{'Test/More.pm'}
58
59 The above is the basis for one of the tests.
60
62 Obviously this only works where the module you're after has its own
63 ".pm" file. If a file defines multiple packages, this won't work.
64
65 This also won't find any modules that are being loaded in some special
66 way, for example using a code reference in @INC, as described in
67 "require" in "perlfunc".
68
70 There are a number of other modules on CPAN which provide the same or
71 similar functionality: App::whichpm, Class::Inspector, Module::Data,
72 Module::Filename, Module::Finder, Module::Info, Module::Locate,
73 Module::Mapper, Module::Metadata, Module::Runtime, Module::Util, and
74 Path::ScanINC.
75
76 I've written a review of all such modules that I'm aware of:
77
78 <http://neilb.org/reviews/module-path.html>
79
80 Module::Path was written to be fast, portable, and have a low number of
81 core-only runtime dependencies. It you only want to look up the path to
82 a module, it's a good choice.
83
84 If you want more information, such as the module's version, what
85 functions are provided, etc, then start by looking at Module::Info,
86 Module::Metadata, and Class::Inspector.
87
88 The following scripts can also give you the path: perldoc, whichpm
89 <https://www.metacpan.org/module/whichpm>.
90
92 <https://github.com/neilbowers/Module-Path>
93
95 Neil Bowers <neilb@cpan.org>
96
98 This software is copyright (c) 2012 by Neil Bowers <neilb@cpan.org>.
99
100 This is free software; you can redistribute it and/or modify it under
101 the same terms as the Perl 5 programming language system itself.
102
103
104
105perl v5.34.0 2021-07-22 Module::Path(3)