Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How can I make one class extend another one?

( #8181=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Apr 20, 2000 at 20:27 UTC
Q&A  > object-oriented programming


Answer: How can I make one class extend another one?
contributed by chromatic

Inheritance is very easy in Perl. Assuming we have a class PerlMonk, we could create a new class called vroom that extends the first:

package vroom; @ISA = qw ( PerlMonk );
From then on, for any method called on an instance of vroom that isn't defined immediately in the class, Perl will search the classes named in @vroom::ISA for those methods.

That is, PerlMonk::dispense_wisdom() is available via $vroom->dispense_wisdom() even if there is no vroom::dispense_wisdom().

Note that the @ISA array can contain lots of parent classes. Note also that they are searched left-to-right, depth first.

Answer: How can I make one class extend another one?
contributed by brother ab

package Child; use base qw(Mother Father);

base pragma not only includes extended classes in @INC, but 'require's them.

Additionally, it is integrated with fields pragma (if you like strict data members protection)

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (9)
    As of 2014-08-28 11:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (259 votes), past polls