Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

RFC: Alter - Perl extension for Alter Ego Objects

by Anno (Deacon)
on Jun 30, 2007 at 15:55 UTC ( #624215=perlmeditation: print w/ replies, xml ) Need Help??

In short, I want to invite those who are sufficiently insterested to take a look at and comment on this module. The download link is here. I intend to put the module up on CPAN.

A month ago I put up a meditation Beyond Inside-Out where I sketched a method to do black-box inheritance in Perl without some of the drawbacks of the inside-out technique. The module above is a worked-out version of that sketch. To summarize:

The basis is a function Alter::ego, which retrieves a class-dependent reference associated with an object. A class uses this alter ego for data storage. The Alter::ego and related functions are XS-implemented. The main disadvantage of Alter is that it is an external XS module, while the inside-out technique is pure perl.

The main advantages of Alter over inside-out objects are

  • An unchanged data model

    The alter ego of an object can be any reference. In particular, it can be a hash and the accustomed method of storing object data in hash elements can be maintained. Only now each class gets to see its individual hash.
  • Garbage collection and thread safety

    Perl's normal garbage-collection and thread cloning fully support Alter-based objects. No DESTROY and CLONE methods are required.
  • Support for Data::Dumper and Storable

    Viewing and serializing objects that use out-of-band data storage need support, which Alter provides
  • Comment on RFC: Alter - Perl extension for Alter Ego Objects
    Select or Download Code
    Re: RFC: Alter - Perl extension for Alter Ego Objects
    by kyle (Abbot) on Jun 30, 2007 at 16:17 UTC

      I don't like the name. If you were looking for a module to do this, and you didn't know what it was called, what would you guess its name to be? I suggest Class::AlterEgo or Object::AlterEgo (to go along with Class::Std and Object::InsideOut).

        I'm afraid you have a point there (afraid, because last-minute name changes are such a pain). I guess it'll be Object::AlterEgo (not Class::...) because, well, it's the object rather than the class that has an alter ego.

        Anno

          I like the name - Alter. But then, maybe Alter::Ego. I don't believe the Object:: prefix adds much to it, since even so, looking at the name, one would ask "what the heck is Alter?". Seeing the ego method inside the package will give the "Aha!" - effect: so it's about the Alter Ego of an object, which is hidden to the outside! Nice.

          But if you want to give it a "conventional" name - what about Object::Magic? It's that - magic, and relies on extension magic.

          --shmem

          _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                        /\_¯/(q    /
          ----------------------------  \__(m.====·.(_("always off the crowd"))."·
          ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        what would you guess its name to be?

        You certainly wouldn't try to find it by guessing its name, but looking it up on CPAN with a few keywords.

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
    Re: RFC: Alter - Perl extension for Alter Ego Objects
    by Joost (Canon) on Jun 30, 2007 at 22:32 UTC
    Re: RFC: Alter - Perl extension for Alter Ego Objects
    by shmem (Canon) on Jul 01, 2007 at 06:28 UTC
      I'm glad it makes its way to CPAN. It is IMHO the best, cleanest and most flexible implementation of objects in perl, them being inside-out adding another point.

      I believe it will give birth to a new way of doing OO in perl, since Alter objects are truely polymorphic by nature. You can tell them to behave like this or that, merely by blessing them into the package which is needed at the time. (Of course, for multiple ego extensions, those packages must be Alter-aware.)

      Keep up the great work, Anno!

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        Thank you. One thing. You said about Alter-based objects

        You can tell them to behave like this or that, merely by blessing them into the package which is needed at the time.

        Re-blessing isn't really involved in accessing alter egos. It's the package the call to ego() is compiled into that determines the class whose ego is seen. That will normaly be the right one when a class compiles its accessors.

        Anno

          It is really common for interesting objects to compile their own accessors. If you require that and break on things compiled in the "wrong" class, much of CPAN is in a world of hurt if applied to your code.

          ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

    Alter - Alter Ego Objects on CPAN
    by Anno (Deacon) on Jul 07, 2007 at 20:01 UTC
      I have put up the Alter module on CPAN, essentially unchanged against the version I introduced a week ago.

      This means I have cast into the wind the sage advice about renaming the module to something that better converys its purpose (but inevitably longer). What's to say? Naming decisions are guts decisions (and particularly hard to make) and that's the way my guts are swinging, if you'll pardon the imagery. The modicum of impudence that lies in occupying a top level name for the little thing is taken into account.

      Anno

      Update: Made mention of Alter a link

        The modicum of impudence that lies in occupying a top level name for the little thing is taken into account.
        It's not a little thing, it's at least of medium size. Not in amount of code, but we're speaking perl, aren't we? I mean impact and importance.

        I'm glad you stuck to the original name. It's so... "let's not talk about technical stuff (yawn). Let's name them as we would do, having a good settled amount of knowledge of concepts and analogies."

        Sorry for me not being able to lay out clearly what that appraisal means... ;-)

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
          It's not a little thing...

          Oh, in terms of impact and importance I believe Alter ought to revolutionize Perl OOP and make it what it was meant to be but never quite was. How's that for a humble opinion?

          But it can only do that as a core module (nobody likes an XS module among the prereqs), and the timing for that is unfortunate with 5.10 on the doorstep and Alter fresh from the drawing board. It'll get lots of time to mature on CPAN or in 5.11, supposing I get it in there. By the time of 5.12 we're all going to use Perl 6 and the point will be moot. :)

          I'm glad you stuck to the original name.

          Not everyone is, I got at least one downvote for my writeup :) Hell, I'm not entirely sure I am happy with the decision. I didn't exactly flip a coin, but it was close.

          Thanks for your support. It's good to know I'm the only one in favor of the Alter technique.

          Anno

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: perlmeditation [id://624215]
    Approved by kyle
    Front-paged by BrowserUk
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (7)
    As of 2014-10-26 06:17 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (151 votes), past polls