package itemDispatch; use strict; use warnings; # The index to the dispatch hash is an item name, the contents is # a list of the callbacks that have been subscribed... my %DISPATCH; # Dispatch hash #################################################################### # Subroutine: Subscribe (external) # This routine subscribes a callback function to a dispatch item # Arguments: # $_[0] - Item Name # $_[1] - Callback function #################################################################### sub Subscribe { my $item = shift || die "missing dispatch item"; my $callback = shift || die "Missing callback function"; push(@{$DISPATCH{$item}}, $callback); } #################################################################### # Subroutine: Activate (external) # This routine activates a dispatch item # Arguments: # $_[0] - Item Name # $_[1]..$[-1] - Callback args #################################################################### sub Activate { # Get arguments... my $item = shift || die "Missing dispatch item"; my @args = @_; # Callback arguments my $coderef; foreach $coderef (@{$DISPATCH{$item}}) { &$coderef(@args); } } 1;