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

(DUP) Re: How to find all the available functions in a file or methods in a module?

by ikegami (Patriarch)
on Nov 11, 2008 at 07:58 UTC ( [id://722800]=note: print w/replies, xml ) Need Help??


in reply to How to find all the available functions in a file or methods in a module?

This node is duplicate. Please ignore.


Have you searched CPAN for modules that walk the symbol table? The following will find all subs currently existing in a package. It can be expanded to walk the entire symbol table. It can be expanded to look at @ISA. There's no way to know whether the subs it finds are methods or not.

use strict; use warnings; use Scalar::Util qw( reftype ); sub is_glob { for (@_ ? $_[0] : $_) { return reftype(\$_) eq 'GLOB'; } } sub get_subs { my ($pkg) = @_; my $symtab = \%::; for (split /::/, $pkg) { $symtab = $symtab->{"${_}::"} or return; } return grep is_glob($symtab->{$_}) && *{$symtab->{$_}}{CODE}, keys %$symtab; } print "$_\n" for sort +get_subs('Foo::Bar');

Interesting tidbit: 'Foo::Bar' can be written as Foo::Bar::.

What other kind of code (besides string eval) can generate methods on the fly? Can we recognize them in a reasonable way?

Assignment of a sub ref to a glob.

Replies are listed 'Best First'.
Re^2: How to find all the available functions in a file or methods in a module?
by LanX (Saint) on Nov 11, 2008 at 14:47 UTC
    There's no way to know whether the subs it finds are methods or not.

    well, you can use B::Deparse on the subref and then regex for $self

    UPDATE: or something like $self and shift or @_ in the same line...

    OK "self" is only a convention but also human intelligence is relying on spotting $self or alike to recognise a method.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://722800]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found