Module::Locate(3)

πŸ“¦
version 1.80 Β· 2 versions
, in fedora 43

πŸ’– Module::Locate πŸ’–

okay bestie, this is the User Contributed Perl Documentation page for Module::Locate, and honestly? it's giving utility icon. it locates modules the exact same way require and use do, no cap. let's get into it 🫢


✨ NAME ✨

Module::Locate β€” locate modules in the same fashion as require and use


🌸 SYNOPSIS 🌸

use Module::Locate qw/ locate get_source /;

add_plugin( locate "This::Module" );
eval 'use strict; ' . get_source('legacy_code.plx');

like. look at that. two lines and you're unstoppable πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so, using locate(), you get back the path that require would find for a given module or filename β€” and lowkey it can also hand you back a filehandle if a reference in @INC has been used. which means you can test whether modules exist, or find their paths, without having to evaluate the code they contain. no executing random code just to check if it's there, we love a safe vibe ✨

this module also comes with accompanying utility functions that are used inside the module itself (except for get_source) and β€” good news β€” they're available for import. so you can absolutely have them too πŸ‘‘


πŸ‘‘ FUNCTIONS πŸ‘‘

import

given function names, the appropriate functions will be exported into the caller's package.

if :all is passed then all subroutines are exported. slay.

the Global and Cache options are no longer supported. see the BUGS section below. rip to those, they had a good run πŸ’€

locate($module_name)

give it a module name as a string (in standard perl bareword format) and it locates the path of the module.

  • in a scalar context: the first path found gets returned
  • in a list context: you get a list of paths where the module was found

also, if references have been placed in @INC then a filehandle will be returned, as defined in the require documentation. and if the module couldn't be located, an empty return is used. no drama, just an empty return πŸ“

as of version 1.7 a filename can also be provided to further mimic the lookup behaviour of require/use. iconic upgrade, honestly.

get_source($module_name)

when provided with a package name, it gets the path using locate(). if locate() returned a path, then the contents of that file are returned by get_source() in a scalar. it's giving "read the whole file, bestie" πŸ“–

acts_like_fh

given a scalar, check if it behaves like a filehandle. it does this in three steps:

  1. first it checks if it is a bareword filehandle
  2. then if it inherits from IO::Handle
  3. and lastly if it overloads the <> operator

if this is missing any other standard filehandle behaviour, please send me an e-mail. fr fr, they want to know πŸŽ€

mod_to_path($module_name)

given a module name, converts it to a relative path β€” e.g Foo::Bar would become Foo/Bar.pm.

note that this path will always use / for the directory separator, even on Windows, as that's the format used in %INC. windows besties, don't come for me, it's just the %INC way πŸ’œ

is_mod_loaded($module_name)

given a module name, return true if the module has been loaded (i.e exists in the %INC hash).

is_pkg_loaded($package_name)

given a package name (like locate()), check if the package has an existing symbol table loaded (checks by walking the %main:: stash). yes, it's literally digging through the stash for you 🧚


🌈 SEE ALSO 🌈

a review of modules that can be used to get the path (and often other information) for one or more modules: <http://neilb.org/reviews/module-path.html>.

App::Module::Locate and mlocate.


πŸ”₯ REPOSITORY πŸ”₯

<https://github.com/neilb/Module-Locate>


πŸ’€ BUGS πŸ’€

in previous versions of this module, if you specified Global => 1 when use'ing this module, then looking up a module's path would update %INC, even if the module hadn't actually been loaded (yet). this meant that if you subsequently tried to load the module, it would wrongly not be loaded. absolute chaos, do not recommend, that's the whole reason Global is gone now 😭

bugs are tracked using RT (bug you can also raise Github issues if you prefer):

<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Module-Locate>


πŸŽ€ AUTHOR πŸŽ€

Dan Brook "<cpan@broquaint.com>"


πŸ’… LICENSE πŸ’…

this is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's the whole page, bestie 🫢 this was written for perl v5.42.0, dated 2025-07-25, and it's the Module::Locate(3) page β€” go forth and locate responsibly βœ¨πŸŒΈπŸ’–

πŸ“¦ where this page comes from