Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

perl inheritance

by Anonymous Monk
on Mar 23, 2012 at 18:13 UTC ( [id://961276]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I'm a bit of a perl OOP newbie, so please excuse me for my lack of experience/know-how.. I'm interested in inheriting from a class where many of the methods return a new copy of itself instead of operating on its own instance. If I derive from this class and I call one of these methods, I'm interested in getting back the derived class (not the base). Is there a way to accomplish this without overriding all the base class methods? (especially since I might want to create many of these types of objects that derive from this base class).

Any help/guidance/examples would be very much appreciated!!!

Replies are listed 'Best First'.
Re: perl inheritance
by jandrew (Chaplain) on Mar 24, 2012 at 04:38 UTC

    Moose is kind of a deep place to jump into, if you are new to OOP in perl, but the things you are asking for are fairly simple to do in the Moose framework.

    In Moose there are separate ideas of a Class and a Role and objects created from these blocks can be joined either at the Class level or the instance level.

    An example from the documentation for basic pre-instance mixing and method conflicts is found in the Moose::Role Manual

    Update: The Moose is flying part I and II are the gentlest introductions that I know of. (by Randal L. Schwartz++)

Re: perl inheritance
by JavaFan (Canon) on Mar 23, 2012 at 18:26 UTC
    Is there a way to accomplish this without overriding all the base class methods?
    That depends on how the superclass has implemented the methods.

    But you may be able to do something like:

    foreach my $method (... list of methods to override ...) { eval <<"EOT"; sub $method { my (\$self, \@args) = \@_; my $result = \$self->SUPER::$method(\@args); bless \$result, __PACKAGE__; \$result; } EOT }

      This is *very* interesting... Thank you! Would this be invoked in the constructor of the derived class?

      Thanks!
        Would this be invoked in the constructor of the derived class?
        Of course not! This is something you need to be done once, and on class level. So, you'd do it outside of any method. That way, it get executed when you use the module.
Re: perl inheritance
by chromatic (Archbishop) on Mar 23, 2012 at 20:38 UTC
    If I derive from this class and I call one of these methods, I'm interested in getting back the derived class (not the base).

    The best way to do this is to fix the superclass to return new objects of the correct class. How does it return new objects?


    Improve your skills with Modern Perl: the free book.

      I'm not sure exactly how I should do that. Would you have the constructor take a base class object and convert it to the derived? Something like the following:

      sub new { if (ref $class eq 'base') { //return derived object built from base class object } //return derived object built from parameters }
      Would invoking this form of the derived class constructor through AUTOLOAD be considered bad practice? This is so that all base class methods do not have to be overridden?

        Would you have the constructor take a base class object and convert it to the derived?

        I'd get the class of the provided object, then call its constructor directly.

        Something like the following...

        No. You're overcomplicating this and confusing yourself. Please post the existing code that constructs a new object so we can all see what you're doing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://961276]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-03-29 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found