Module::Path(3pm)

Module::Path3pm

Get the full path to a locally installed module

Usage

 use Module::Path 'module_path';
 
 $path = module_path('Test::More');
 if (defined($path)) {
   print "Test::More found at $path\n";
 } else {
   print "Danger Will Robinson!\n";
 }

Overview

This module provides a single function, "module_path()", which takes a module name and finds the first directory in your @INC path where the module is installed locally. It returns the full path to that file, resolving any symlinks. It is portable and only depends on core modules.

It works by looking in all the directories in @INC for an appropriately named file:

•

Foo::Bar becomes "Foo/Bar.pm", using the correct directory path separator for your operating system.

•

Iterate over @INC, ignoring any references (see "require" in "perlfunc" if you're surprised to hear that you might find references in @INC).

•

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.

•

If a directory in @INC is a symlink, then we resolve the path, and return a path containing the linked-to directory.

•

If no file was found, return "undef".

I wrote this module because I couldn't find an alternative which dealt with the points listed above, and didn't pull in what seemed like too many dependencies to me.

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:

  module_path('Test/More.pm') eq $INC{'Test/More.pm'}

The above is the basis for one of the tests.

Repository

<https://github.com/neilbowers/Module-Path>

Releases

6 releases since Debian 8 (Jessie); the text changed 3 times.

  1. 8
  2. 9–10
  3. 11
  4. 12–13

Bugs

Obviously this only works where the module you're after has its own ".pm" file. If a file defines multiple packages, this won't work.

This also won't 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".

Author

Neil Bowers <[email protected]>

From libmodule-path-perl 0.19-3 in Debian 13 (Trixie). Parsed by carta from the page's man source.