I never felt the need to do such, but here is how I would do it.

Since every symbol table of packages your current package knows about is in the current package's symbol table, and since subclassing basically consists in including the base class in a packages's @ISA array and overriding some methods, I'd look at the current symbol table for packages it contains and see if the @ISA array of those packages contains the package looked for.

Something like this:

#!/usr/bin/perl -w use strict; package Bar; use base 'IO::Handle'; package Element; sub new { my $type = shift; my $self = {}; bless $self, $type; } package Circle; use vars qw(@ISA); @ISA = qw( Element ); package Square; use vars qw(@ISA); @ISA = qw( Element ); package Line; use vars qw(@ISA); @ISA = qw( Element ); package main; sub list_of_derived_classes { my $class = shift; my @pkg_keys; { no strict 'refs'; @pkg_keys = grep { /::$/ } keys %{__PACKAGE__.'::'}; } my @result; for (@pkg_keys) { no strict 'refs'; if (defined ${$_}{ISA}) { push @result, $_ if grep {/^$class$/} @{${$_}{ISA}}; } } s/::$// for @result; @result; } for my $class (qw(Element IO::Handle)) { print "$class\: ",join(', ', list_of_derived_classes($class)),$/; } __END__ Element: Circle, Line, Square IO::Handle: Bar

Converting the symbol table lookups into a form which passes strict 'refs' is left as an excercise to the reader ;-)


In reply to Re: OO-Perl Question: How Do I get a List of derived Classes for a Base Class? by shmem
in thread OO-Perl Question: How Do I get a List of derived Classes for a Base Class? by BluePerl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.