rwadkins has asked for the wisdom of the Perl Monks concerning the following question:

I tried to do something that didn't work.

I have a package Foo and a package Bar. Bar ISA Foo. Foo defines method Baz. I want to call Bar->Baz and as part of Baz' steps call Foo->Baz. However, Foo->Baz doesn't seem to exist. It's as if, through inheritance, Baz gets slipped into the symbol table only at the lowest level.

I'm trying to get at (lexically scoped?) data defined in each package along the inheritance chain. Is this possible or even a good idea?

Replies are listed 'Best First'.
Re: Recursion up The Inheritance Chain
by keszler (Priest) on Dec 18, 2009 at 17:43 UTC
    Are you calling SUPER?
    #!perl use strict; package Foo; sub Baz { my $self = shift; print "In Foo->Baz\n"; } sub Quux { my $self = shift; print "In Foo->Quux\n"; } package Bar; use vars qw/@ISA/; @ISA = qw(Foo); sub new { my $class = shift; return bless {}, $class; } sub Baz { my $self = shift; print "In Bar->Baz\n"; $self->SUPER::Baz; } package main; my $snafu = Bar->new; $snafu->Baz; $snafu->Quux; __END__ In Bar->Baz In Foo->Baz In Foo->Quux

    Update:added Foo->Quux

      I am calling SUPER, but Baz isn't defined in Bar. It's only defined in Foo. Defining it in Baz would be redundant and if there were more objects in the chain between Foo and Bar, then I wouldn't be able to step up each one. which is wny I wanted recursion.
        I misunderstood your question - I thought you had a sub Baz in both and couldn't get to Foo's. I updated my previous reply with Foo->Quux; no SUPER call needed, just $snafu->Quux.
Re: Recursion up The Inheritance Chain
by WizardOfUz (Friar) on Dec 18, 2009 at 17:54 UTC

    You might also want to take a look at perltooc.

Re: Recursion up The Inheritance Chain
by WizardOfUz (Friar) on Dec 19, 2009 at 11:36 UTC

    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