[Chicago-talk] Object interogation

Randal L. Schwartz merlyn at stonehenge.com
Tue Oct 25 06:29:10 PDT 2005

>>>>> "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 . "::" };


my @methods = $some_object->methods_of_class;
my @others = Some::Class->methods_of_class;

Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

More information about the Chicago-talk mailing list