#!/usr/bin/perl -w use strict; use Time::HiRes qw(sleep); use Getopt::Long; use Pod::Usage; =head1 NAME onchange - a little program to run a command when a file changes =head1 SYNOPSIS Run C<make> command whenever a source file is updated: $ onchange make *.c *.h & Run C<pod2html> on a .pod file whenever it is updated: $ onchange 'pod2html foo.pod > foo.html' foo.pod & Run C<make> whenever any file in the current directory, or any directory below, is updated (ignoring files starting with .): $ onchange make . =head1 OPTIONS A couple options are available for your use: =over =item --verbose See the command everytime it runs. =item --sleep 0.1 Set the granularity of the check for file changes in seconds. Defaults to 0.5. =back =cut my $verbose = 0; my $sleep = 0.5; pod2usage(2) unless GetOptions('verbose' => \$verbose, 'sleep=f' => \$sleep); pod2usage("Too few arguements.") unless @ARGV >= 2; my ($command, @input_files) = @ARGV; # expand directories in @input_files list with a recursive walk and # fill in @mtimes with mtimes for files my (@files, @mtimes); while (my $f = shift @input_files) { if (-f $f) { push(@files, $f); push(@mtimes, (stat(_))[9]); } elsif (-d _) { opendir(DIR, $f); push @input_files, map { "$f/$_" } grep { $_ !~ /^\./ } readdir(DIR); } else { die "File '$f' does not exist or is not a file/directory."; } } while(1) { for (my $x = 0; $x < @files; $x++) { my $mtime = (stat($files[$x]))[9]; next unless $mtime; if ($mtime != $mtimes[$x]) { print STDERR "Running '$command'\n" if $verbose; system($command); } $mtimes[$x] = $mtime; } sleep($sleep); }

In reply to onchange - a script to run a command when a file changes by samtregar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.