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;
####
package doWork;
use strict;
use Tk;
use itemDispatch;
my $PROGRESS = 0;
sub StartWork {
my $increment = shift || die "Missing Increment";
my $c=0;
while ($c<99) {
sleep 1;
itemDispatch::Activate('PROGRESS', $increment);
$c += $increment;
}
}
1;
##
##
use strict;
use Tk;
use Tk::StatusBar;
use itemDispatch;
use doWork;
# Subscribe to PROGRESS events...
itemDispatch::Subscribe('PROGRESS', \&_updateProgress);
my $PROGRESS = 0;
my $INCREMENT = 5;
my $mw = MainWindow->new();
$mw->Button( -text => 'Start', -command =>
# Publish a START event...
sub{ doWork::StartWork($INCREMENT)})->pack();
my $sb = $mw->StatusBar();
$sb->addProgressBar(
-length => 60,
-from => 0,
-to => 99,
-variable => \$PROGRESS,
);
MainLoop();
sub _updateProgress {
my $progress = shift || die "Missing Progress\n";
print STDERR "Updating progress by $progress\n";
$PROGRESS += $progress;
$mw->update;
}