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

Hi all, I am relativly new to perl and need a shove in the right direction concerning class methods. How do you denote whether a method in a subclass should overwrite or augment the same method in its parent class?

EX: Class B & C are inherited from class A. Class A has a method called "sub 123 {}". In Class B I want sub 123 to do something completely differnt. However, in Class C I want sub 123 to do what it does in Class A ples a little more.

I assume there is a perldoc somewhere that explains this but I have not found it yet. Could I get some directions please?

thanks,

ch

Replies are listed 'Best First'.
Re: Class Methods
by borisz (Canon) on Jul 27, 2004 at 22:20 UTC
    package A; sub new { bless {}, shift } sub x { "A::x"; } package B; our @ISA="A"; package C; our @ISA='A'; sub x{ shift->SUPER::x . " C::x" } package main; print A->new->x, "\n"; print B->new->x, "\n"; print C->new->x, "\n";
    here is a example. see also perldoc perlboot.
    Boris
Re: Class Methods
by friedo (Prior) on Jul 27, 2004 at 23:55 UTC
    All methods are inherited, unless they are overidden. Thus, you can have something like the following:

    package A; sub foo { print "this is from class A\n"; } package B; use base 'A'; sub foo { print "this is from class B\n"; } sub bar { print "this is from class B, too\n"; } package C; use base 'A'; sub bar { print "Finally, this is from class C.\n"; }

    In this example, package C inherits the foo method from package A, because it is declared a subclass with use base and does not define its own method foo. Package B, on the other hand, defines its own foo method, which overrides the parent class.

    See perlboot and perltoot for more information about doing OOP in Perl.

Re: Class Methods
by hv (Prior) on Jul 28, 2004 at 10:25 UTC

    In a class inheriting from class A, any method you declare will replace class A's method of the same name in your new class, so your class B's 123 need do nothing special. In class C, you can access the superclass's method using the special pseudo-package SUPER:::

    package C; our @ISA = qw/ A /; sub 123 { my $self = shift; # let my superclass do its thing first my $result = $self->SUPER::123(@_); # now I'll do a bit ... }

    Note that occasionally you may need to be careful to propagate your caller's context to the SUPER:: call, if the supermethod acts differently depending on it. Eg:

    sub 123 { my $self = shift; my(@array, $scalar); if (wantarray) { @array = $self->SUPER::123(@_); } elsif (defined wantarray) { $scalar = $self->SUPER::123(@_); } else { $self->SUPER::123(@_); } # do more stuff return wantarray ? @array : $scalar; }

    For more complex class hierarchies, or if generating class methods at runtime, you may find the specific semantics of SUPER:: insufficient. If so, you may also want to look at the NEXT module.

    Hugo

Re: Class Methods
by Anonymous Monk on Jul 28, 2004 at 15:00 UTC
    Thanks for the info. I should be able to figure it out from here. I'll have to do some testing but I think SUPER was the piece that I was missing. -ch