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

My parent class lives in its own package, imaginatively called "Base". Here is the new call from Base:

sub new {
my $class = shift;
my $self = bless {}, $class;
return $self;
}

In my inheriting class, I call the parent class like this:
use base ("Leads::Export::Base");

My new call in the inheriting class looks like this:
sub new
{
my ( $class, %parameters ) = @_;
my $self = bless( {}, ref( $class ) || $class );
return $self;
}

I'm trying to call a parent method like this:
Leads::Export::Base->write_export_log( $log_vars );
# no warn...no err...just doesn't do it

I've also tried:
$self->write_export_log( $log_vars );
# same here. Warning before the call and after the call AND inside the call. Before and After warns fire, one inside the call is skipped.

Obviously I'm doing something wrong here. I'm a PHP programmer and I've only been writing Perl for a month, so please be gentle (and very thorough).

Replies are listed 'Best First'.
Re: Package can't find Parent Class
by ELISHEVA (Prior) on Sep 18, 2009 at 13:59 UTC

    You want $self->SUPER::write_export_log($log_vars). "SUPER::blah(...)" is a special incantation that calls the parent's definition of "blah".

    Perl always passes the thing to the left of -> as the first parameter to your method subroutine, so any method call that acts on "$self" should always begin $self->blah(...). You didn't say what kind of warnings you got when you tried to call $self->write_export_log(...) but I'm guessing that you got something like "Deep recursion on subroutine..." if you did it inside of

    package SubclassOfBase; sub write_export_log { my $self = shift; $self->write_export_log(); }

    Leads::Export::Base->write_export_log($log_vars) didn't do what you wanted either because it was passing the classname as the first parameter to write_export_vars instead of the object ($self).

    Best, beth

Re: Package can't find Parent Class
by ~~David~~ (Hermit) on Sep 18, 2009 at 13:57 UTC
    Hmm.. I have almost the exact same thing as you ( even the name of my base class is base!. Here is how I did it:
    Parent Class ( contains logging method ):
    package MARS::Base; sub log{... }
    Child Class:
    package MARS::Klarf1_2; @UNIVERSAL::ISA = qw(MARS::Base);
    Main Script:
    my $K = MARS::Klarf1_2->new(); $K->log( 'Blahh blahh blahh' );
    This works fine for me...