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


In reply to Re: Class Methods by hv
in thread Class Methods by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.