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

Esteemed Monks,

I am rapidly learning about Perl's Object Oriented features. Right now I want to write a module that has some 'common methods' that I would like to be able to inherit in other modules. For example:

I have a Tk based programme. In it I have created whole sections of the UI as objects and this has worked well. But I now find that some parts of these various UI's are common to each other, particularly things like listcmd and browsecmd methods for BrowseEntry's for example.

How do I handle this?

Do I put the methods in a module called, say "handy_methods" and inherit it using @ISA = qw( handy_methods );?

Then I assume that once 'inherited' that the methods have access to the attributes of the blessed hash? So that I can directly refer to attributes rather than having to pass references into the method.

I am reading chapter 6 of thedamian's book on Object Oriented Perl but I am getting a little lost in here!

jdtoronto

Replies are listed 'Best First'.
Re: Help me understand inheritance please!
by diotalevi (Canon) on Nov 04, 2004 at 18:28 UTC
    Since your object isn't a handy_method, you should consider simply exporting these things instead. ISA is only valid when you really and truely do have the situation where one object is a more specific example of the parent class. You'll probably want to consider using delegation before considering inheritance.
      Oh yes!

      So you suggest that my 'toolchest' of things be put into a 'normal' module and be imported into the OO module. That makes sense!

      My problem is that I read the book and nod sagely, but when it comes to sitting at the keyboard it doesn't make that much sense any more! Thanks for the clarification.

      jdtoronto

        my 'toolchest' of things be put into a 'normal' module and be imported into the OO module

        Your toolchest doesn't necessarily have to be a procedural ("normal") module, it too can be object oriented. diotalevi is only suggesting that the relationship between your toolchest module and your application module doesn't appear to be one where inheritance is appropriate.

        Inheritance is for when one thing is another thing, where as delegation is when one thing has another thing. These are commonly refered to as is-a and has-a relationships. This is why inheritance in Perl is done through the @ISA array. Delegation in Perl can be accomplished simply by storing one object as an attribute in another object, and calling the first object's methods as appropriate.

        To further reinforce the point, consider describing your situation in these terms. Does it make more sense to say "my application is a toolbox" or "my application has a toolbox"? Compare to a frequent example where inheritance is used: "this cat is a mammal" vs "this cat has a mammal." That should help you decide which is appropriate.

        My problem is that I read the book and nod sagely, but when it comes to sitting at the keyboard it doesn't make that much sense any more!

        Such is the curse of OO design guidelines: putting them into practical use.

        "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: Help me understand inheritance please!
by dragonchild (Archbishop) on Nov 04, 2004 at 20:14 UTC
    A lot depends on what kind of commonalities you have. If significant portions of the
    • interface are similar, you don't have inheritance - you have an API
    • functionality are similar, you want to have a utilities module
    • functionality and interface are similar, you should consider inheritance from some common base class(es)

    A utilities module shouldn't contain any references to the innards of any object. It should literally be a bunch of subs along the lines of:

    sub min { $_[0] < $_[1] ? $_[0] : $_[1] }
    These are subs that do something useful, but are completely atomic - they don't have any reference points outside themselves.

    Now, you mention listcmd and browsecmd. I don't know Tk, but it sounds like they both deal with commands, in some sense. So, maybe you don't actually have inheritance as much as delegation. Have you considered abstracting out some cmd object and having listcmd and browsecmd both contain an instance of cmd?

    From what I'm guessing, the list part would be inheritable. So, let's say you had a listfoo and a listbar. You would have a list baseclass that both would inherit from, plus foo and bar classes that they would delegate to.

    DOes that make sense?

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Help me understand inheritance please!
by rinceWind (Monsignor) on Nov 04, 2004 at 18:59 UTC
    If you are looking to group Tk widgets together, you want to consider Tk mega-widgets. You can make new widgets by deriving from an existing widget, or by composition. I refer you to Steve Lidie's excellent book Mastering Perl/TK

    This is a somewhat different approach from the standard @ISA type inheritance that is Perl OO

    --
    I'm Not Just Another Perl Hacker

      Thanks rinceWind,

      Another interesting suggestion. By your method I could take my BrowseEntry, wrap it up in a MegaWidget with the code for its associated events (listcmd and browsecmd in this case) and then use it wherever that functionality is needed. That would work, and as I think about it I could pass a coderef into the megawidget for another method that needs to be run whenever a value is selected from the drop-down.

      My copy of Steve Lidie's Mastering Perl/Tk is right beside me and is falling apaprt in some places! Almost time to go and buy another.

      jdtoronto

Re: Help me understand inheritance please!
by marcelo.magallon (Acolyte) on Nov 04, 2004 at 19:52 UTC
    Have you considered using Spiffy (CPAN for it)?