Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Perl Monk, Perl Meditation
 
PerlMonks  

List of loaded subroutines

by slloyd (Hermit)
on Jul 28, 2005 at 07:04 UTC ( #478852=perlquestion: print w/ replies, xml ) Need Help??
slloyd has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to figure out a way to print the list of subroutines that are loaded via use and require of the currently running script. Is this possible?
use strict; use Win32; require '/mysubs.pl'; # print a list of subroutines that are currently loaded ???

Comment on List of loaded subroutines
Download Code
Re: List of loaded subroutines
by Fang (Pilgrim) on Jul 28, 2005 at 07:14 UTC
Re: List of loaded subroutines
by davido (Archbishop) on Jul 28, 2005 at 07:19 UTC

    You could try the B::Xref module:

    perl -MO=Xref myscript.pl

    For non-trivial scripts the output can be quite verbose. Fortunately, the POD for the module mentions the -o option which allows you to dump the output to a file instead of the screen.


    Dave

Re: List of loaded subroutines
by xdg (Monsignor) on Jul 28, 2005 at 11:20 UTC

    You can look at the symbol table of a package for code entries. E.g. for "main"

    no strict 'refs'; for ( keys %{"main::"} ) { print "$_\n" if defined *{"main::$_"}{CODE}; }

    If you want to know subroutines of everything loaded, then you can use the %INC hash, convert back to module names, and search through all of them for subroutines:

    my @subs; for my $mod ( keys %INC ) { $mod =~ s!/!::!g; $mod =~ s/.pm$/::/; { no strict 'refs'; for ( keys %{$mod} ) { push @subs, $mod . $_ if defined *{$mod . $_}{CODE}; } } } print "$_\n" for @subs;

    (Note that this won't find subroutines that are explicitely loaded into a package that isn't named the same as the file that was required or used.)

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://478852]
Approved by monkfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (15)
As of 2014-04-18 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (471 votes), past polls