Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Structure of a large Perl GUI application

by dragonchild (Archbishop)
on Feb 18, 2009 at 14:57 UTC ( #744779=note: print w/replies, xml ) Need Help??

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).

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?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://744779]
[shmem]: well finally.
[stevieb]: nice! I just finished a GPS "take me home" device last week, and did a bunch of software updates to it yesterday. I also created a pseudo chip with an Arduino to simulate an IC, where it responds to register read/writes over the I2C bus...
[stevieb]: ...from an I2C master. It's ugly and there are many changes I'm going to make, but I had not done anything like it before. It's designed for my RPi:: automated test platform; a system that does CI on *all* my RPi modules.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2017-06-25 22:38 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (572 votes). Check out past polls.