Module::Data - Introspect context information about modules in @INC
version 0.011
use Module::Data; my $d = Module::Data->new( 'Package::Stash' ); $d->path; # returns the path to where Package::Stash was found in @INC $d->root; # returns the root directory in @INC that 'Package::Stash' was found inside. # Convenient trick to discern if you're in a development environment my $d = Module::Data->new( 'Module::Im::Developing' ); if ( -e $d->root->parent->subdir('share') ) { # Yep, this dir exists, so we're in a dev context. # because we know in the development context all modules are in lib/*/* # so if the modules are anywhere else, its not a dev context. # see File::ShareDir::ProjectDistDir for more. } # Helpful sugar. my $v = $d->version;
Returns the package the Module::Data instance was created for. ( In essence, this will just return the value you passed during new, nothing more, nothing less.
Module::Data
new
my $package = $md->package
Check to see if the module is already recorded as being loaded in %INC
%INC
if ( $md->loaded ) { say "$md was loaded"; }
Require the module be loaded into memory and the global stash.
my $mod = Module::Data->new( 'Foo' ); # nothing much happens. $mod->require; # like 'require Foo';
Returns the "package" name itself for convenience so you can do
my $mod = Module::Data->new('Foo'); $mod->require->new( %args );
A Path::Tiny object with the absolute path to the found module.
my $md = Module::Data->new( 'Foo' ); my $path = $md->path;
$path is computed optimistically. If the "package" is listed as being "loaded", then it asks %INC for where it was found, otherwise, the path is resolved by simulating perl's path look up in @INC via Path::ScanINC.
$path
perl
@INC
Path::ScanINC
Returns the base directory of the tree the module was found at. ( Probably from @INC );
local @INC = ( "somewhere/asinine/", "somewhere/in/space/", # Where Lib::Foo::Bar is "somethingelse/", ); my $md = Module::Data->new( "Lib::Foo::Bar"); $md->path ; # somewhere/in/space/Lib/Foo/Bar.pm my $root = $md->root # somewhere/in/space
If the module appears to be already loaded in memory:
my $v = $md->version;
is merely shorthand for $package->VERSION;
However, if if the module is not loaded into memory, all efforts to extract the value without loading the code permanently are performed.
Here, this means we compute the path to the file manually ( see "path" ) and parse the file with Module::Metadata to statically extract $VERSION.
Module::Metadata
$VERSION
This means you can unleash this code on your entire installed module tree, while incurring no permanent memory gain as you would normally incur if you were to require them all.
require
Kent Fredric <kentnl@cpan.org>
This software is copyright (c) 2013 by Kent Fredric <kentnl@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Module::Data, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Module::Data
CPAN shell
perl -MCPAN -e shell install Module::Data
For more information on module installation, please visit the detailed CPAN module installation guide.