Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

a list of functions within a pm

by shrubbery (Acolyte)
on Jul 19, 2001 at 22:48 UTC ( #98187=perlquestion: print w/replies, xml ) Need Help??
shrubbery has asked for the wisdom of the Perl Monks concerning the following question:

Howya all ye grand Perl-monketeers,

I'm working on a perl module that neads to read some configuration numbers. Here's my problem, I need to find a list of every subroutine in that pm. So if I have say:
sub blah1 { ... }
sub blah2 { ... }

then is there a way I can get blah1, blah2 in a list while I'm coding in that pm?? Thanks for any help.


Replies are listed 'Best First'.
Re: a list of functions within a pm
by TheoPetersen (Priest) on Jul 19, 2001 at 23:09 UTC
    If your package only contains subs (no package variables), you can just use the package's symbol table:
    package foo; sub one { return 1 }; sub two { return 2 }; sub subs { no strict; return keys %foo::; }
    You'll also get a BEGIN entry (at least, with 5.6.1 I did).
      There's no guarantee that methods for that package have been defined outside the .pm file. There's also no guarantee that the .pm file doesn't contain more than one package. Granted both are less than elegant, but still possible.
Re: a list of functions within a pm
by merlyn (Sage) on Jul 20, 2001 at 09:28 UTC
Re: a list of functions within a pm
by mikfire (Deacon) on Jul 19, 2001 at 23:09 UTC
    I am not exactly sure how you want this list displayed, but the attached code is one I have used before to accomplish a similar job.

    It expects the name of the file to be searched as the only command line parameter. It will also skip any subroutine whose name begins with an underscore - as I said, this is ripped from a larger program ( the second toy ) doing something similar.

    I also cause it to print the line number where the sub definition begins.

    #!/usr/bin/perl -w use strict; my $source = shift @ARGV || ''; my %keyword = (); unless ( $source ) { print "Usage: $0 <file>\n"; exit; } open SRC, $source or die "Couldn't open $source : $!"; while( <SRC> ) { my ( $package, $name ); $package = $1 if ( ! $package && /^package\s+(.+);/ ); next unless ( /^sub\s+(.+){\s*$/ ); $name = $1; next if ( substr($name,0,1) eq "_" ); $name =~ s/\s+$//; $keyword{$name} = $.; } close SRC; for ( keys %keyword ) { print "$keyword{$_}:$_\n"; }

    Update: Do NOT use Opera for Solaris when submitting - it seems to add a whole lot of newlines. Sorry for the mess.


Re: a list of functions within a pm
by shrubbery (Acolyte) on Jul 19, 2001 at 23:14 UTC
    Sorry for any confusion. I meant I needed those subroutine names for coding not just searching in an editor. So I'd need the names in a list/array in that PM as a global variable. I'll give Theo's method a shot first. Thanks! Mike
Re: a list of functions within a pm
by davorg (Chancellor) on Jul 20, 2001 at 16:22 UTC

    All of the suggestions you've been given so far are necessarily incomplete. The best you can do is to get a snapshot of the subroutines that are defined in a module's synbol table at a given time. That's not necessarily the complete list of subroutine calls that the module will react to.

    In Re: Parse/Validate Code I listed some of the most obvious exceptions that will cause you problems.


    Perl Training in the UK <>

Re: a list of functions within a pm
by arturo (Vicar) on Jul 19, 2001 at 23:08 UTC

    I'm not sure I understand the question. It *sounds* like a question about how to use your editor. Read the manuals for the editor you're using to program. any editor worth using has a "find" and a "repeat last find" facilities.

    Or have I misunderstood your question?

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: a list of functions within a pm
by Anonymous Monk on Jul 20, 2001 at 15:21 UTC
    use Devel::Symdump
Re: a list of functions within a pm
by bschmer (Friar) on Jul 20, 2001 at 18:05 UTC
    Here's some code that you may find useful. You need to take into account one of the previous comments that this is only a snapshot, but it should help you.
    #!/usr/bin/perl use strict; sub sub1 { print "In sub1\n"; } sub sub2 { print "In sub2\n"; } sub sub3 { print "In sub3\n"; } sub MergeHashes { my ($hr1, $hr2) = @_; my (%retval, $nkey, $key); print "hr1 = $hr1, hr2 = $hr2\n"; foreach $key (keys %{$hr2}){ $retval{$key} = $$hr2{$key}; } foreach $key (keys %$hr1){ if (!defined($retval{$key})){ $retval{$key} = $$hr1{$key}; } } \%retval; } sub showhash { my ($hr, $hn) = @_; my %hash = %$hr; my @subs; no strict; return if ($checked{$hr}); $checked{$hr} = 1; my $symname; foreach $symname (sort keys %hash){ local *sym = $hash{$symname}; if (@sym){ push(@subs, "array $symname"); } elsif (%sym){ push(@subs, "hash $symname"); showhash(\%sym, $symname); } elsif (defined(&sym)){ push(@subs, "sub $symname"); } else { push(@subs, "var $symname"); } } if (scalar @subs){ print "Package $hn => "; foreach $symname (sort @subs){ print "\t", $symname, "\n"; } } } my %hash = ( h1 => { a => "A", c => "C", e => "E", g => undef, i => undef, }, h2 => { a => "A2", b => "B", d => "D", g => "G2", h => undef, } ); showhash(\%main::, "%main::");
Re: a list of functions within a pm
by frankus (Priest) on Jul 23, 2001 at 15:18 UTC
    You're Ned Flanders and I claim my 5 GBP ;-), re:
    <Simpsons character="Ned Flandidly-Anders" >
    Howya all ye grand Perl-monketeers

    Fooling aside, there is some cool advice here,so I'll add this (possibly redundant) stoopid suggestion:

    • If someone else wrote it, you only want to use the subs they mention in the pod. Tinkering with other stuff is not recommended.
    • If you wrote it; I advocate using dispatch tables ( a hash of function references ), that way the most you ever have to do is list the keys to the hash containing the functions for that module. There are lotsa reasons for using em.


    Brother Frankus.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://98187]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2017-03-28 00:45 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (326 votes). Check out past polls.