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

jZed and tyx: Thanks for quick response. Sorry am still bit lost.
I have AppMod.pm and I need to override AppMod::request() created MyAppMod.pm in this MyAppMod.pm is the following correct?

package MyAppMod; use strict; # this part I am not sure. use AppMod; # or do I use this line? our @ISA=qw(AppMod); #and then do I still say: my $am = new AppMod; # and my @ch_p = change_print(); # change_print() exists in AppMod too. sub change_print { my @output; # do stuff (which calls functions and global variable from AppMod) return (@output); }


Would this be correct?
Thanks once again for quick help/responses!
--nikie

Replies are listed 'Best First'.
Re: overridden and inheritance
by atemon (Chaplain) on Aug 22, 2007 at 04:47 UTC

    Hi,

    Is there a reason why you inherit AppMod to MyAppMod and create an object of AppMod at the same time ? If its just for overriding the subroutines, its not required. Consider the following.

    { package Animal; sub speak { my $class = shift; print "a $class goes ", $class->sound, "!\n" } } # Animal package from before { package Mouse; @ISA = qw(Animal); sub sound { "squeak" } sub speak { my $class = shift; print "a $class goes ", $class->sound, "!\n"; print "[but you can barely hear it!]\n"; } } Mouse->speak;
    This will result in
    a Mouse goes squeak! [but you can barely hear it!]
    Here, Mouse has its own speaking routine, so Mouse->speak doesn't immediately invoke Animal->speak For more details of overriding, have a look at Overriding Methods on CPAN.

    Cheers !

    --VC

    Note: Above example is from perlboot



    There are three sides to any argument.....
    your side, my side and the right side.

Re: overridden and inheritance
by GrandFather (Saint) on Aug 22, 2007 at 05:06 UTC

    Not sure if this is a useful answer, however you seem to be struggling with overriding methods so consider:

    # file noname1.pm: use strict; use warnings; package Hello; sub new { my $type = shift; # Call as: Hello->new (); my $class = ref $type || $type; return bless {}, $class; } sub hi { my ($self, $tail) = @_; $tail ||= ''; return 'Hello' . $tail; } 1; package World; use noname1; use base qw(Hello); sub new { my $type = shift; # Call as: World->new (); my $class = ref $type || $type; return Hello::new ($class); } sub hi { my ($self, $tail) = @_; $tail ||= ''; # Now call up to get base class work done return $self->Hello::hi (' World' . $tail); } 1; #file noname.pl: use strict; use warnings; use noname1; my $hi = World->new (); # Create derived class object print $hi->hi ('. How goes it?'); # Call method on derived class objec +t

    Prints:

    Hello World. How goes it?

    DWIM is Perl's answer to Gödel
      my $class = ref $type || $type;

      Nix that; you don't need it.

      sub new { my $type = shift; # Call as: World->new (); my $class = ref $type || $type; return Hello::new ($class); }

      You don't need that either. Just delete the whole thing.

      # Now call up to get base class work done return $self->Hello::hi (' World' . $tail);

      This is a lot better as:

      return $self->SUPER::hi( ' World' . $tail );

      ... or even:

      use SUPER; # ... return $self->SUPER( ' World' . $tail );
      This is helpful and thanks for the example!!. and yes that's what I was running into problem and understanding.
      Thanks all!! it has been very helpful.