Print out modules used during runtime?

O

Omar Zakaria

Hello, all. I was wondering if there's a way, without downloading any
special modules, or packages, to have a perl script print out the list
of packages or modules it imported (i.e. anything I "use"'d or
"require"'d). The script is actually compiled to an .exe, so it can't
open itself and search for "use". Any ideas? I've tried fiddling with
package namespaces and reference tables, but I can't seem to find where
the information is stored, orif its even there.

As an example, I'd like to know how to get the script below to print out
"foo/bar.pm, bar/foo.pm" or something along those lines (assuming, of
course, those modules exist):

__BEGIN__

use foo::bar;
use bar::foo;

#Insert awesome perl program here

....

__END__


I'd appreciate any help I could get in the matter.
 
P

Paul Lalli

Omar said:
Hello, all. I was wondering if there's a way, without downloading any
special modules, or packages, to have a perl script print out the list
of packages or modules it imported (i.e. anything I "use"'d or
"require"'d).

%INC is probably what you're looking for. It keeps a list of all
modules used/required as keys, with the values being the paths to
those modules. Note, however, that %INC will have not only the modules
your script specifically used, but also those modules used by a module
your script used:

$ perl -e' use CGI; print "$_ => $INC{$_}\n" for keys %INC;'
warnings/register.pm =>
/opt2/Perl5_8_4/lib/perl5/5.8.4/warnings/register.pm
Carp.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/Carp.pm
vars.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/vars.pm
strict.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/strict.pm
Exporter.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/Exporter.pm
constant.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/constant.pm
warnings.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/warnings.pm
CGI/Util.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/CGI/Util.pm
overload.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/overload.pm
CGI.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/CGI.pm

See perldoc perlvar for more information about %INC.

Paul Lalli
 
J

John Bokma

Paul Lalli said:
%INC is probably what you're looking for. It keeps a list of all
modules used/required as keys, with the values being the paths to
those modules. Note, however, that %INC will have not only the modules
your script specifically used, but also those modules used by a module
your script used:

$ perl -e' use CGI; print "$_ => $INC{$_}\n" for keys %INC;'
warnings/register.pm =>
/opt2/Perl5_8_4/lib/perl5/5.8.4/warnings/register.pm
Carp.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/Carp.pm
vars.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/vars.pm
strict.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/strict.pm
Exporter.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/Exporter.pm
constant.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/constant.pm
warnings.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/warnings.pm
CGI/Util.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/CGI/Util.pm
overload.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/overload.pm
CGI.pm => /opt2/Perl5_8_4/lib/perl5/5.8.4/CGI.pm

See perldoc perlvar for more information about %INC.

I think the problem is:

#Insert awesome perl program here

and the fact that it's an exe.

http://www.die.net/doc/linux/man/man1/strace.1.html

I am sure there is something similar for Windows (tracing which files are
opened by an exe).
 
O

Omar Zakaria

Paul said:
%INC is probably what you're looking for. It keeps a list of all
modules used/required as keys, with the values being the paths to
those modules. Note, however, that %INC will have not only the modules
your script specifically used, but also those modules used by a module
your script used:

Perfect. Thanks a million.
 
M

Michele Dondi

Hello, all. I was wondering if there's a way, without downloading any
special modules, or packages, to have a perl script print out the list
of packages or modules it imported (i.e. anything I "use"'d or
"require"'d). The script is actually compiled to an .exe, so it can't

I *like* to play tricks with code in @INC:

C:\WINDOWS>perl -le "use lib sub { !warn pop }; use CGI"
CGI.pm at -e line 1.
Carp.pm at -e line 1.
Exporter.pm at -e line 1.
CGI/Util.pm at -e line 1.
vars.pm at -e line 1.
warnings/register.pm at -e line 1.
warnings.pm at -e line 1.
constant.pm at -e line 1.
overload.pm at -e line 1.

(DISCLAIMER: optimized for brevity, as I've broken my left arm and I'm
currently waiting for it to be surgically operated!)

Of course this kind of trick is more complex than plainly inspecting
%INC and in this case it has no advantage over the latter. You may
like it if you are interested in the order in which modules are
searched or if you want your report for selected C<use> statements
only.


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top