I don’t know a tool like nodemon for Perl, unless it restarts a program when it changes. Here I knocked together.
use strict;
use warnings;
use v5.10;
use File::Monitor;
use Child qw(child);
sub start_program {
my $program = shift;
return child {
exec $^X, $program, @ARGV;
};
}
sub restart_program {
my($program, $child) = @_;
$child->kill("TERM");
return start_program($program);
}
sub monitor_program {
my $program = shift;
my $monitor = File::Monitor->new;
my $child = start_program($program);
say "PID: ".$child->pid;
$monitor->watch($program, sub {
$child = restart_program($program, $child);
});
while(1) {
sleep 1;
$monitor->scan;
}
}
monitor_program(shift);
This can be made more efficient by replacing File :: Monitor with something connecting to the OS X file system event service.
source
share