Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

List of loaded subroutines

by slloyd (Hermit)
on Jul 28, 2005 at 07:04 UTC ( [id://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 ???

Replies are listed 'Best First'.
Re: List of loaded subroutines
by davido (Cardinal) 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.

Re: List of loaded subroutines
by Fang (Pilgrim) on Jul 28, 2005 at 07:14 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-26 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found