[Chicago-talk] Object interogation
Steven Lembark
lembark at wrkhors.com
Thu Oct 27 10:24:48 PDT 2005
-- Jay Strauss <me at heyjay.com>
> Randal L. Schwartz wrote:
>>>>>>> "Jay" == Jay Strauss <me at heyjay.com> writes:
>>
>>
>> Jay> Can you do the same with Perl? I know you can ->can, but that
>> requires Jay> prior knowledge of method names. I can't think of how
>> you'd get object Jay> attributes.
>>
>> Yes. You can look at the subroutines defined in the package of the
>> class. However, you will not be able to tell which of those are
>> "really" methods and which are merely private subroutines.
>>
>> sub UNIVERSAL::methods_of_class {
>> my $this = shift;
>> my $class = ref $this || $this;
>> no strict 'refs';
>> grep exists &$_, keys %{ $class . "::" };
>> }
> thanks Randal. I was thinking there may be a more core built-in
> facility than having to do it manually for each class.
The entire symbol table is fully exposed: there is
nothing more "built in" than that.
Why would you want a list of all the methods supported
by an object (vs. asking whether the object supports a
given method using $obj->can( $method ))?
As for "private" subs, they can easily be privatized
via anon sub's (or a hash of them) and taken out of
the symbol table if you are going to use this kind of
interrogation on a regular basis.
--
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