[Israel.pm] getting class methods

Gaal Yahas gaal at forum2.org
Tue Aug 10 08:09:34 PDT 2004


On Tue, Aug 10, 2004 at 06:01:06PM +0300, Gaal Yahas wrote:
> > given instance of class or class name  how can i get all the methods belonging to this instance/class
> 
> perl -le 'sub f{}; do { print if defined *{"${class}::$_"}{CODE} } for keys %{$class."::"}'
> 
> To get $class, evaluate $class = ref($proto)||$proto .

This was written in haste, but is due some notes.

The above works by peeking at a namespace's symbol table. perl5 does not
differentiate between subroutines and methods, so you will get all named
subs. Also, this doesn't do any more complex OOP; if this is a base
class, you do not get any of its default (super) implementations.
Likewise, if a module uses AUTOLOAD, obviously you can't get at the code
in advance since that is determined at run time. The inheritance
limitation can be overcome by walking @INC; the AUTOLOAD one cannot.

-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list