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

Implementing an event-driven object model in Perl

by jkva (Chaplain)
on Jun 23, 2006 at 18:56 UTC ( #557291=perlquestion: print w/replies, xml ) Need Help??

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


Recently I bought the 1994 edition of the "Gang of Four" Design Patterns book. I've been reading it and it has taught me a lot of things already that I did not know before.

For a project I continually work on to exercise my Perl skills, I am thinking of implementing the following (my methodology might still be way off) :

I'd like to have objects that are in a certain state. When interacted with their state changes and they will call some kind of notification method on themselves. By calling this, let's say, $self->notify(), other objects will recieve this and act accordingly. This should form some kind of response-to-notification event model.

I am not sure how I would implement such in Perl, therefore I would like to lay this before the hive mind that is PerlMonks, and see what your thoughts are on this.

Thanks for reading,

-- jkva

Update :
So far I am thinking that all objects classes are subclasses of a class which has a notify() method, which calls some kind of hub class, which knows about all objects and notifies them, this way there would be some kind "distributed awareness". Just doodling with my mind here, it might be very bad :)

Replies are listed 'Best First'.
Re: Implementing an event-driven object model in Perl
by jeffa (Bishop) on Jun 23, 2006 at 19:05 UTC

    Your title sounds like POE, but your description sounds like the Observer Pattern ... you have the GoF book in front of you, and they give a recipe for generic implementation. Why don't you try that and post some code for us? :)


    (the triplet paradiddle with high-hat)
      Thanks for the reply! However, I am still in the first chapter, I am slowly progressing. But I'll cheat and skip ahead to that :)
Re: Implementing an event-driven object model in Perl
by GrandFather (Saint) on Jun 23, 2006 at 19:09 UTC

    You may wish to consider whether individual objects notify all other objects, or a selected list of objects, or objects of a specific class.

    Something to watch for in the "notify all" scenario is that the notification doesn't trigger another notification of the same type by the original object - that way leads to great activity with no forward motion. You may wish to tag notifications with a sender ID as a partial solution to that problem, although if another object responds to the notification by doing some work that notifies the original object (perhaps indirectly!), then you still have a problem.

    DWIM is Perl's answer to Gödel
      Yes, of course. Good point. I guess certain objects would have defined to which events they should have to respond, and by what, of course. We don't want theoretically infinite notification. Also perhaps rule out any "tag-back" (as in playing tag, no tagging of $tagger).
Re: Implementing an event-driven object model in Perl
by zentara (Archbishop) on Jun 23, 2006 at 20:33 UTC
    If you have the Gtk2 system installed, you might want to look at it's base object class called Glib. Read "perldoc Glib". Glib itself does not need the Xserver, and it has alot of powerful minds actively working on it.
    ABSTRACT This module provides perl access to GLib and GLibís Object libraries. GLib is a portability and utility library; GObject provides a generic type system with inheritance and a powerful signal system. Together these libraries are used as the foundation for many of the libraries that make up the Gnome environment, and are used in many unrelated projects.
    I have a simple example at Roll your own Event-loop

    I'm not really a human, but I play one on earth. flash japh
Re: Implementing an event-driven object model in Perl
by Joost (Canon) on Jun 23, 2006 at 23:18 UTC
      What's the bug in Class::Observable? I'm not overly familiar with it, but I'm curious. If it's esoteric then perhaps point me towards a thread.


Re: Implementing an event-driven object model in Perl
by demerphq (Chancellor) on Jun 26, 2006 at 10:27 UTC

    Even though its only indirectly related I thought it was worth making a minor point:

    Dont get too caught up in the OO aspect of Design Patterns. Perls functional abilities bring an entirely different view to how things like this can work. For instance the observer pattern works just fine with closures as callbacks, and has the advantage of being much more flexible and powerful.

    IMO it would be a good idea to read Higher Order Perl and then go back to Design Patterns. I think the insight youll gain from HOP will be very useful in "perlizing" the stuff in design patterns.


Re: Implementing an event-driven object model in Perl
by Moron (Curate) on Jun 27, 2006 at 11:26 UTC
    I don't understand why other objects should receive notification of a state change unless you really mean that there is a unique state for the class. But if so, then you should want to avoid duplicating the information and instead use a separate class for aggregate information. For example, suppose we are making a route finder or solving the TSP with such an object model, in which our objects are nodes in a network and trial routes exploring the network which collapse whenever they meet each other and spawn children at each node to explore each possible direction to take. A node will have one of three states which are caused by the progress of the trial routes: untouched, touched, established and eliminated. So we would find it much more efficient to store that state in the node object, not in every single trial route object that has been through the node, even though it is the progress of the trial routes that caused the node state to change.


    Free your mind

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2022-08-08 00:37 GMT
Find Nodes?
    Voting Booth?

    No recent polls found