package Dispatcher; use strict; use threads::shared; sub new { my($inv,%arg) = @_; my $class = ref $inv || $inv; # I forgot the name of the function. # It's provided by the new version of threads::shared. # It returns a shared version of data, very convenient, # so we don't have to recursively share things. my $data = shared_data { %arg, _listeners => { 'EVENT_FOO' => [], 'EVENT_BAR' => [] } }; return bless $data,$class; } sub addListener { my($self,$e_name,$ref) = @_; die if ! exists $self->{_listeners}{$e_name}; # here will throw an error # as the sub ref cannot be put into shared store push @{$self->{_listeners}{$e_name}}, $ref; } sub dispatchEvent { my $event = shift; my $e_name = $event->eventName; die if ! exists $self->{_listeners}{$e_name}; foreach my $func ( @{$self->{_listeners}{$e_name};} ) { &$func($event); } }