Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Recursion up The Inheritance Chain

by WizardOfUz (Friar)
on Dec 19, 2009 at 11:36 UTC ( [id://813513]=note: print w/replies, xml ) Need Help??


in reply to Recursion up The Inheritance Chain

Maybe this is what you want:

#!/usr/bin/perl use strict; use warnings FATAL => 'all'; use 5.009_005; # or MRO::Compat; package MyClassDataHelper; { sub get_all_class_data { my $invocant = shift; my $class = ref( $invocant ) || $invocant; my @all_class_data; { no strict 'refs'; for my $package ( @{ mro::get_linear_isa( $class ) } ) { next unless defined *{"${package}::get_class_data"}{CO +DE}; push @all_class_data, $package->get_class_data; } } return @all_class_data; } } package MyClass::A; { use base qw( MyClassDataHelper ); sub get_class_data { return 'MyClass::A data' } } package MyClass::B; { use base qw( MyClass::A ); sub get_class_data { return 'MyClass::B data' } } package MyClass::C; { use base qw( MyClass::B MyClass::A ); sub get_class_data { return 'MyClass::C data' } } package MyClass::D; { use base qw( MyClass::C MyClass::A MyClass::B ); } # Tests print map { "$_\n" } MyClass::D->get_all_class_data; # Output: # MyClass::C data # MyClass::B data # MyClass::A data print map { "$_\n" } MyClass::C->get_all_class_data; # Output: # MyClass::C data # MyClass::B data # MyClass::A data print map { "$_\n" } MyClass::B->get_all_class_data; # Output: # MyClass::B data # MyClass::A data print map { "$_\n" } MyClass::A->get_all_class_data; # Output: # MyClass::A data

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found