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

plz explain it, if this makes any sense, i hope u'll understand what im trying to do here.
package AGENT; sub new { .... } sub Callme { .... } 1;
package TOOLS; use AGENT; sub new : AGENT::new # should initialize the AGENT::new { .... } sub Callme { .... } 1;
use TOOLS; my $tools = new TOOLS; $tools->SUPER::Callme(); # it should call the AGENT::Callme $tools->Callme();
Does perl has SUPER to call the base class if i ahave to packages in one perl module
sorry for type errors.

2006-11-11 Retitled by g0n, as per Monastery guidelines
Original title: 'Advanced OOP'

Replies are listed 'Best First'.
Re: How to do basic inheritance in Perl OO?
by Joost (Canon) on Nov 06, 2006 at 18:58 UTC
    my $tools = new TOOLS; $tools->SUPER::Callme(); # it should call the AGENT::Callme $tools->Callme();
    If you need to do this outside of the TOOLS package, you're doing something very wrong. The code that's using the $tools object should not need to know or care what $tool's inheritance chain is. If your TOOL's Callme() cannot work without also using AGENT::Callme(), then you should set it up so that AGENT::Callme is called automatically from TOOL::Callme, not rely on the user to do it for you. Same goes for new().
    package TOOLS; sub new { my ($package) = @_; my $self = $package->SUPER::new(); # do other stuff, or remove this whole sub if # you're not acutally doing anything in this # method and let AGENT's new() method do all the work. return $self; } sub Callme { my ($self) = @_; $self->SUPER::Callme(); # do stuff. }
    As explained by someone else above, you can't (and shouldn't) use SUPER outside of the package that is the subclass.
Re: How to do basic inheritance in Perl OO?
by samtregar (Abbot) on Nov 06, 2006 at 18:37 UTC
    There's a number of problems in your code. First off, as has already been pointed out, you need to set @ISA to establish inheritence, either directly or with 'use base'. Second - this isn't valid Perl syntax:

       sub new : AGENT::new

    I don't know what you thought that would do, but I doubt it's going to help! Finally, this won't work in most cases:

       $tools->SUPER::Callme();

    In Perl, SUPER only works inside the sub-class's package. It looks like it should be evaluating $tool's @ISA but in fact it's evaluating @__PACKAGE__::ISA. In most cases this is what you want since you don't usually allow clients to skip up the inheritence tree, but in your example that would appear to be the case.

    I suggest you slow down and pick up a good book on object-oriented Perl - "Programming Perl" or "Object Oriented Perl" are both good. You need to learn the basics before you start free-stylin'.

    -sam

Re: How to do basic inheritance in Perl OO?
by Sidhekin (Priest) on Nov 06, 2006 at 18:22 UTC

    You want use base 'AGENT'; or something like @TOOLS::ISA = ('AGENT');

    use AGENT; on its own just loads (if necessary) and imports from (if any exports exist in) AGENT.

    Update: Yikes, that's @ISA, not @isa, of course.

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!