Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Shlomi Fish (918)

Shlomi Fish
  shlomif@iglu.org.il
http://www.shlomifish.org/
AOL IM: ShlomiFish (Add Buddy, Send Message)
Yahoo! ID: shlomif2 (Add User, Send Message)
Jabber: ShlomiFish@jabber.org

I'm a hacker of Perl, C, Shell, and occasionally other languages. Perl is my favourite language by far. I'm a member of the Israeli Perl Mongers, and contribute to and advocate open-source technologies. Technorati Profile [technorati.com]

Journal of Shlomi Fish (918)

Monday June 11, 2007
10:56 AM

Plugins for Helper Objects

[ #33492 ]

In this entry, I'd like to discuss an OOP technique of having a class with plugins, where the plugins specify plugins for the objects used ("has-a") by instances of the class.

This occured to me in Test-Run, where I implemented several classes which I called "Structs" (like in C) which wrapped around a hash ref using accessors. The instances of these classes were referenced by the main class ("Test::Run::Core"). I graudually moved more and more methods into the helper classes, if I saw they only referred to their own data.

Now, I have some plugins of the Test::Run::Core class. It happened to me at least once now, that after I moved a few methods to the helper classes, the plugin broke, because the method in question was no longer present in the main class. I had to find a way to extend the helper classes, and it made me thinking that I'd like to have some plugins for it too.

Plugins in Perl, at least as implemented by Catalyst and Test-Run, are done by creating an empty class (let's say MyPluggableClass), adding all the plugins, in front to @MyPluggableClass::ISA, and adding the master class which the plugins enhance at the end. So @MyPluggableClass::ISA will look something like:

( qw(Plugin::Foo Plugin::Bar Plugin::Quux MasterClass) )

Now, I'd like a way for plugins of the Test::Run main class, to add plugins to its helper classes. I already have some code to traverse the inheritance tree and collect an array from the individual methods (shamelessly borrowed from a similar paradigm in Class::Std). Using it, I can collect all the plugins, append the master class at the end, and initialise a new instance of the helper class.

I can even abstract this functionality into its own meta class. So my questions are:

  1. Was this done before?
  2. Is there a better way to do it?
  3. Does Moose support a similar paradigm?
  4. Should I rethink my strategy?

I think one big monolithic class is not the way to go, so I'd appreciate any comments.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Was this done before?

    Sure.

    Is there a better way to do it?

    I'm in the camp that believes that there's always a better way to do it.

    Does Moose support a similar paradigm?

    Sure [cpan.org].

    Should I rethink my strategy?

    No idea, since I don't know T:R.

    .phaylon

    --
    Ordinary morality is for ordinary people. -- Aleister Crowley
    • Was this done before?

      Sure.

      OK. :-)

      Is there a better way to do it?

      I'm in the camp that believes that there's always a better way to do it.

      OK, so I suppose that as far as consulting you, my way is good enough.

      Does Moose support a similar paradigm?

      Sure [cpan.org].

      Nice. In any case, I was looking for a built-in or default way for plugins of main classes to specify the plugins of the helper classes, without me having to write too much helper code. What you showed was a generic plugin mechanism for Moose, not such a way. What I want is still doable, but should still be done explicitly.

      Should I rethink my strategy?

      No idea, since I don't know T:R.

      .phaylon

      OK, no problem.