| dotReader documentation | Contained in the dotReader distribution. |
MultiTask::Minion - a worker
my $worker = MultiTask::Minion->new();
Creates a new minion class, defining work() and other methods inline.
my $worker = MultiTask::Minion->make(sub {
return(work => sub {...})
});
$self->_standard_attributes;
$self->_make_method($name, $subref);
$minion->quit;
$minion->DESTROY;
Eric Wilhelm <ewilhelm at cpan dot org>
http://scratchcomputing.com/
If you found this module on CPAN, please report any bugs or feature requests through the web interface at http://rt.cpan.org. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
If you pulled this development version from my /svn/, please contact me directly.
Copyright (C) 2006 Eric L. Wilhelm, All Rights Reserved.
Absolutely, positively NO WARRANTY, neither express or implied, is offered with this software. You use this software at your own risk. In case of loss, no person or entity owes you anything whatsoever. You have been warned.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| dotReader documentation | Contained in the dotReader distribution. |
package MultiTask::Minion; $VERSION = eval{require version}?version::qv($_):$_ for(0.10.1); use warnings; use strict; use Carp; use Class::Accessor::Classy; rw 'on_quit'; rs 'done' => \(my $set_done); no Class::Accessor::Classy;
sub new { my $class = shift; my $self = {}; my $new_class = "$self"; { $new_class =~ s/HASH\(([^\)]*)\)/${class}::$1/ or croak("cannot transform $self into a package"); my $isa = do { no strict 'refs'; \@{"${new_class}::ISA"}; }; push(@$isa, $class); # You're one of us now... } bless($self, $new_class); return($self); } # end subroutine new definition ########################################################################
sub make { my $package = shift; my ($subref) = @_; ((ref($subref) || '') eq 'CODE') or croak("not a code reference"); my $self = $package->new; my %atts = $subref->($self); foreach my $att ($self->_standard_attributes) { if($atts{$att}) { $self->_make_method($att, delete($atts{$att})); } } keys(%atts) and croak("unsupported attributes ", join(", ", keys(%atts))); return($self); } # end subroutine make definition ########################################################################
sub _standard_attributes { my $self = shift; return(qw( start work finish quit )); } # end subroutine _standard_attributes definition ########################################################################
sub _make_method { my $self = shift; my ($name, $subref) = @_; ($name =~ m/^[a-z_][\w]*$/i) or croak("'$name' not a valid name"); my $class = ref($self); ($class =~ m/::0x/) or croak("'$class' is invalid"); if(my $super_sub = $class->can($name)) { no strict 'refs'; *{$class . '::SUPER_' . $name} = $super_sub; } no strict 'refs'; defined(&{$class . '::' . $name}) and croak("cannot overwrite $name"); *{$class . '::' . $name} = $subref; } # end subroutine _make_method definition ########################################################################
sub quit { my $self = shift; if(my $on_quit = $self->on_quit) { $on_quit->($self); } my $class = ref($self); if($class =~ m/::0x/) { # delete our methods foreach my $att ($self->_standard_attributes) { no strict 'refs'; if(defined(&{$class . '::' . $att})) { delete(${$class . '::'}{$att}); } } } $self->$set_done(1); } # end subroutine quit definition ########################################################################
sub DESTROY { my $self = shift; #warn "destroy $self\n"; delete($self->{$_}) for(keys(%$self)); if(1) { # cleanup namespace my $package = ref($self); $package =~ m/^(.*::)([^:]+)$/ or die; my $parent = $1; my $inner = $2 . '::'; # don't kill-off permanent packages! # TODO use something that's not pattern-based? ($inner =~ m/^0x/) or return; # warn "not destroying $package"; my $pack; { no strict 'refs'; $parent = \%{"$parent"}; #$innerp = \%{"$inner"}; $pack = \%{"${package}::"}; } #warn join(",", keys(%$parent)); #warn join(",", keys(%$pack)); #warn join(",", keys(%{$parent->{$inner}})); delete($parent->{$inner}); #warn join(",", keys(%$parent)); } return; } # end subroutine DESTROY definition ########################################################################
# vi:ts=2:sw=2:et:sta 1;