[tpm] Detect a module's directory

Stuart Watt stuart at morungos.com
Mon Feb 22 11:42:45 PST 2010

Digimer wrote:
> Is there a reliable* way to detect or read the directory that a perl 
> module is in? '$ENV{PWD}' returns the directory of the script loading 
> the module, not of the module itself. My Google-fu is failing me as 
> finding a solution, too.
>   The reason I need this is that my core module reads in a 
> configuration file from the root of the module's directory. I could 
> hard-code it, but I would rather it be flexible in case it gets moved 
> down the road. 
If you've loaded it, %INC contains what you need. However, 
Class::Inspector is probably more helpful, and probably does everything 
you need in one go. For example:

print Class::Inspector->resolved_filename( 'Log::Log4perl' );
print Class::Inspector->loaded_filename( 'Log::Log4perl' );

These are slightly different. One says where it would be loaded from, 
and the second where it was loaded from. The first can change, in 
theory, if people change @INC.

All the best

More information about the toronto-pm mailing list