[Chicago-talk] Object interogation

Steven Lembark lembark at wrkhors.com
Thu Oct 27 10:21:18 PDT 2005

-- Jay Strauss <me at heyjay.com>

> Hi,
> During the Ruby presentation, we saw that Ruby provides for object 
> interrogation/inspection/whatever_its_called - that is you can ask an 
> object what methods it responds to, and I think ask it what it's 
> attributes are too.
> Can you do the same with Perl?  I know you can ->can, but that requires 
> prior knowledge of method names.  I can't think of how you'd get object 
> attributes.

You can use:

    $obj->can( $method )

to test a single method. It'll return a coderef or 
undef. In most [any?] cases you don't want to poll 
the object for names of what it can do but would
want to ask "does the object support this method?"
The can operator does this nicely.

AUTOLOAD technically makes it impossible to give a
list of all the methods supported by an object (or
class) since the AUTOLOAD can implement any methods
it likes on the fly.

For example, Object::Trampoline is nothing BUT an
AUTOLOAD block and supports any constructor you 
ask it to :-) Ditto the Shell module.

You can look into the symbol table using %:: or 
a few modules that interrogate it. A depth-first
search of %:: along @ISA will tell you what 
methods are available to a given object's class.

Steven Lembark                                       85-09 90th Street
Workhorse Computing                                Woodhaven, NY 11421
lembark at wrkhors.com                                     1 888 359 3508

More information about the Chicago-talk mailing list