Nodemon-like tools for perl

Is there something like nodemonthat keeps track of file changes and restarts the perl script?

My perl script is just a while loop.

I tried using it for google, but the results are relevant mod_perlor irrelevant.

Any ideas?

I am using mac os

+4
source share
1 answer

I don’t know a tool like nodemon for Perl, unless it restarts a program when it changes. Here I knocked together.

#!/usr/bin/env perl

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.

+3
source

Source: https://habr.com/ru/post/1614859/


All Articles