Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
pubsub is really the way to go for this. The Javascript frameworks provide this sort of thing and it's really useful. Essentially, you create a place for subscribers to go subscribe. Each subscriber will register a callback with the central office, in essence "When XYZ is published to, please call this function with whatever arguments are provided." Then, the publisher will, when publishing to the event, ring up the central office and say "I want to publish to XYZ with ($foo, $bar)". The central office then trips the callbacks with ($foo, $bar).

Something like the following could be used:

package CentralOffice; my %events; my %names; my $counter = 0; sub subscribe { my $self = shift; my ($event, $callback, $name) = @_; $event = lc $event; $name ||= sprintf( "subscription_%04d", $counter++ ); $name = lc $name; $events{$event} ||= {}; # This is the subscribe step $events{$event}{$name} = $callback; return $name; } sub unsubscribe { my $self = shift; my ($event, $name) = @_; $event = lc $event; $name = lc $name; # This is the unsubscribe step return delete $events{$event}{$name}; } sub publish { my $self = shift; my ($event, @args) = @_; return unless %{$events{$event}}; # This is the publish step. $_->(@args) for values %{$events{$event}}; return 1; }
Usage should be pretty self-explanatory.

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

In reply to Re: Structure of a large Perl GUI application by dragonchild
in thread Structure of a large Perl GUI application by DarrenM

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found