Dear Monks,
I write a module that is intended to wrap all the stuff for running a command into an OO-module. However, I cant't figure out how to write a signal handler that can access the modules methods and variables.
The code looks basically like this:
package MyPack;
use strict;
use warnings;
use Posix ":sys_wait_h";
use constant STATUS_STOPPED=>'stopped';
use constant STATUS_RUNNING=>'running';
$SIG{CHLD}=\&reap_child;
sub new{
my $class=shift;
my $self= bless{
_pid=>undef
, _status=>STATUS_STOPPED
}, $class;
return $self;
}
sub run{
my $self=shift;
my $command=shift;
croak "Process: no comand ist given \n" unless $command;
my $child= fork();
die "Process: Forking error $? \n" unless defined $child;
if($child>0){
$self->{_pid}=$child;
$self->{_status}=STATUS_RUNNING;
}
else {
exec($command);
}
}
sub reap_child{
if (waitpid($self->{_pid},WNOHANG) > 0) {
$self->{_status}=STATUS_STOPPED;
$self->{_pid}=undef;
}
}