http://www.perlmonks.org?node_id=958782

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

Answered! Many thanks again!


No Moose, just bare perl... More's the pity.

I have a child object inheriting a parent object - I can call methods that are on the parent object, not the child, access properties defined on the parent object, etc.

I can override a parent method, but in this case I don't want to do that - I want to call the parent object's method and then execute more child-specific code after that. I have the code to call the parent object's constructor from within my childs constructor, but the same code doesn't seem to work on other methods, which is both annoying and confusing me.

What I have for my constructor:
sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = $class->SUPER::new(@_); bless $self, $class; # do other stuff here for the child object... return $self; }
Works a treat. However if I have something like
sub reset { my $self = shift; my $class = ref($self) || $self; $class->SUPER::reset(@_); $self->setMatchMethod(\&multiline_match); return $self; }

perl barfs up and reports "Can't use string (<object name>) as a HASH ref while "strict refs" in use at ..." on the $class->SUPER::reset(@_) line.

It's even truncating the object name, which is REALLY confusing me - instead of "...::DocumentManager::PlainText" for the string it's unhappy with, it's saying "...::DocumentManager::Pl". (full object path redacted for the sake of my employer).

In a fit of wild flailing I tried $class::SUPER::reset($self, @_); but that just gives a syntax error.

So I'm clearly Not Doing This Right.

Replies are listed 'Best First'.
Re: Extending parent object method?
by chromatic (Archbishop) on Mar 09, 2012 at 20:40 UTC

    Here's the problem:

          my $class = ref($self) || $self;

    Within reset(), you're trying to get the class of the object. That'll be a string. The parent's reset() method wants an object invocant.

    The good news is that you never have to write that ref($self) || $self. Delete it (even from your constructor) and this error should go away.

    Your new() could be:

    sub new { my $class = shift; my $self = $class->SUPER::new(@_); # do other stuff here for the child object... return $self; }

    There's no need to rebless it, provided the parent constructor uses two-argument bless.


    Improve your skills with Modern Perl: the free book.

      Bingo.

      Now I'm slightly baffled that "STRING::CLASS"->SUPER::new(@_); even works, but I think I'm not going to sweat that until I recover from Daylight Savings Jetlag."

        I'm slightly baffled that "STRING::CLASS"->SUPER::new(@_); even works.

        Perl's method dispatcher handles the SUPER:: case in a special fashion. It looks weird, but it works.