Here is a simple example of using inheritance:

Say you have some stock footage for an action-adventure TV show, and you want to re-use it several times without anyone noticing. This week, we are going to flip a jeep and distract some guards.

use strict; use ordnance; my $T= new ordnance(); $T->flip_jeep(); sleep 2; $T->firecracker();
This week's episode uses the ordnance module, which inherits from the stock_footage module.

Here is ordnance.pm:

package ordnance; use stock_footage; @ISA=qw(stock_footage); sub new { my $this = shift; my $class = ref($this) || $this; my $self = {}; bless $self, 'ordnance'; } sub flip_jeep { my $this = shift; my $class = ref($this) || $this; print "pull grenade pin\n"; sleep 2; $class->SUPER::flip_jeep(); } sub firecracker { my $this = shift; my $class = ref($this) || $this; print "light fuse\npsssss....\n"; sleep 2; print "Bang! Bang! Bang!\n"; $class->SUPER::distraction(); } 1;
and here is stock_footage.pm:
package stock_footage; sub flip_jeep { print "And the jeep flies into the air and flips!\n"; } sub distraction { print "And the guards are momentarily distracted!\n"; }
So, someone can write this week's show using my ordnance routines, and they won't know that they are actually using stock_footage. Of course, I would write pod documentation for my ordnance routines so that the caller doesn't have to look at the ordnance code to know how to use it.

I have not written much code that uses inheritance, but I'm sure that other monks can correct me if I have messed up my code. There are many things you can do with inheritance, just as there are many things you can do with a for loop. I would like an even more brief example, if someone can figure one out.

UPDATE:Fixed problems pointed out by tilly and petdance.

It should work perfectly the first time! - toma


In reply to Re: Understanding what Inheriting is. by toma
in thread Understanding what Inheriting is. by Mr.T

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.