in reply to Understanding what Inheriting is.

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

Replies are listed 'Best First'.
Re (tilly) 2: Understanding what Inheriting is.
by tilly (Archbishop) on Aug 13, 2001 at 15:59 UTC
    Prototypes are ignored on method calls, so your prototypes are doing nothing. You should drop them. (I prefer not using them ever. But that is me.)

    As for short examples, well OO is aimed at making longer code maintainable, not at making short examples. But still in Re (tilly) 8: To sub or not to sub, that is the question? I gave some code that has appropriate method calls inserted and some explanation for how you would use inheritance.

Directions on military supplies
by petdance (Parson) on Aug 13, 2001 at 18:49 UTC
    ++ on the explanation, toma. Quick aside, though:

    "ordinance" means "an authoritative decree or direction." The word you want is "ordnance", which means "military supplies including weapons, ammunition, combat vehicles, and maintenance tools and equipment."

    xoxo,
    Andy
    --
    <megaphone> Throw down the gun and tiara and come out of the float! </megaphone>

Re: Re: Understanding what Inheriting is.
by Sifmole (Chaplain) on Aug 13, 2001 at 16:45 UTC
    Why do you use SUPER:: in your calls to stock_footage methods of flip_jeep and distraction?

    Since ordinance @ISA = qw(stock_footage); couldn't you use

    $this->flip_jeep()

    instead of
    my $class = ref($this) || $this; $class->SUPER::flip_jeep();

    What am I missing about your example?

      I tried your suggestion and got an infinite loop. I pulled many grenade pins!

      It should work perfectly the first time! - toma