in reply to Structure of a large Perl GUI application
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).
My criteria for good software:
Something like the following could be used:
Usage should be pretty self-explanatory.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; }
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
|
---|
In Section
Seekers of Perl Wisdom