Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Finding classes which are inherited from a given class

by fergal (Chaplain)
on Aug 06, 2004 at 09:15 UTC ( [id://380479]=note: print w/replies, xml ) Need Help??


in reply to Finding classes which are inherited from a given class

The hard way is to look inside %:: (or %main:: if you prefer) for keys ending in ::. Let's say that the only package that has been loaded is A::B::C. Look inside %main:: and you will see a variety of things, including a key "A::". This tells you that something has been loaded into the package A. Next you should look at @A::ISA to see if A inherits from your class. Then you look inside %A:: and you will find a key "B::", again check @A::B::ISA, finally you look inside %A::B:: and you will find a key "C::" so check @A::B::C::ISA. When you look inside %A::B::C:: you don't find any keys ending in :: so you're done.

Code would look a little like

check_pkg("::"); sub check_pkg { my $pkg = shift; # ::main:: and :: are the same and we don't want an inifinite loop return if $pkg eq "::main::"; foreach my $sub_pkg (grep /::$/, keys %{$pkg}) { my $full_pkg = "$pkg$sub_pkg"; print "checking $full_pkg\n"; print "$full_pkg inherits\n" if check_isa($full_pkg); check_pkg($full_pkg); } } sub check_isa { # check @{$pkg."::ISA"} to see if it inherits }
The easy way is to use a module to help with this, I thought there was one but I can't find it!

Update: as an aumsement, here's a version that doesn't use recursion

my @to_check = ("::"); while (my $pkg = pop @to_check) { next if $pkg eq "::main::"; # because ::main:: and :: are the same foreach my $sub_pkg (grep /::$/, keys %{$pkg}) { my $full_pkg = "$pkg$sub_pkg"; print "checking $full_pkg\n"; print "$full_pkg inherits\n" if check_isa($full_pkg); push(@to_check, $full_pkg); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://380479]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found