|o||Foo::Bar becomes Foo/Bar.pm, using the correct directory path separator for your operating system.|
|o||Iterate over @INC, ignoring any references (see require in perlfunc if youre surprised to hear that you might find references in @INC).|
|o||For each directory in @INC, append the partial path (Foo/Bar.pm), again using the correct directory path separator. If the resulting file exists, return this path.|
|o||If a directory in @INC is a symlink, then we resolve the path, and return a path containing the linked-to directory.|
|o||If no file was found, return undef.|
The distribution for Module::Path includes the mpath script, which lets you get the path for a module from the command-line:
% mpath Module::Path
The module_path() function will also cope if the module name includes .pm; this means you can pass a partial path, such as used as the keys in %INC:
The above is the basis for one of the tests.
Obviously this only works where the module youre after has its own .pm file. If a file defines multiple packages, this wont work.
This also wont find any modules that are being loaded in some special way, for example using a code reference in @INC, as described in require in perlfunc.
There are a number of other modules on CPAN which provide the same or similar functionality: App::whichpm, Class::Inspector, Module::Data, Module::Filename, Module::Finder, Module::Info, Module::Locate, Module::Mapper, Module::Metadata, Module::Runtime, Module::Util, and Path::ScanINC.
Ive written a review of all such modules that Im aware of:
Module::Path was written to be fast, portable, and have a low number of core-only runtime dependencies. It you only want to look up the path to a module, its a good choice.
The following scripts can also give you the path: perldoc, whichpm <https://www.metacpan.org/module/whichpm>.
Neil Bowers <firstname.lastname@example.org>
This software is copyright (c) 2012 by Neil Bowers <email@example.com>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
|perl v5.20.3||MODULE::PATH (3)||2015-03-16|