in reply to subclass discovery at runtime
You'll need to (recursively) get all defined namespaces, and then check all of them to see which ones are a subclass of ourselves... The code below seems to work...
package SuperClass; sub subclasses { return grep { UNIVERSAL::isa($_,__PACKAGE__) && $_ ne __PACKAGE__ } main::packages(); } package SubClass; @ISA=qw(SuperClass); package main; sub packages { return packages({},\%main::) unless @_; my ($packages,$in) = @_; while(my ($package,$table) = each %$in) { next unless $package =~ /(.*)::$/; next if exists $packages->{$table}; $packages->{$table} = $1; packages($packages,$table); } return values %$packages; } $\=$,="\n"; print SuperClass::subclasses();
Because a class doesn't (and shouldn't) know about it's subclasses, you'd probably have to do something like this:
Naturally untested, but it might just work...my @subclasses = grep { /^(.*?)::$/ && # it's a package entry UNIVERSAL::isa($1,__PACKAGE__) # entry is subclass of me! } keys %main:: ; # for everything in main namespace
Update: isa probably needed a package name
Another update: my 'solution' isn't even close, and I don't know why...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: subclass discovery at runtime
by Aristotle (Chancellor) on Jan 27, 2003 at 16:46 UTC | |
by Gilimanjaro (Hermit) on Jan 27, 2003 at 17:26 UTC | |
by Aristotle (Chancellor) on Jan 27, 2003 at 17:40 UTC | |
by Gilimanjaro (Hermit) on Jan 27, 2003 at 18:11 UTC | |
by Aristotle (Chancellor) on Jan 27, 2003 at 23:00 UTC | |
|