Peamasii has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I was wondering if anyone knows an easy way to auto-merge multiple text files when they are updated. Using a setting file containing: #part1.txt #part2.txt #part3.txt I would want to auto-generate an output.txt file containing their contents whenever they are edited. Is there a way Perl can monitor a file and when it's changed, re-generate the output?

Replies are listed 'Best First'.
Re: Auto-Merging textfiles
by roboticus (Chancellor) on Dec 17, 2010 at 16:59 UTC

    Peamasii:

    You mean something like this?

    my %files = map { $_=>undef } ('part1.txt', 'part2.txt', 'part3.txt'); while ( my $fl=1 ) { for (keys %files) { my @t=stat $_; if ($t[9]>$files{$_}) { $files{$_}=$t[9]; $fl=0; } } unless ($fl) { $fl = join(" ", "cat", keys %files); `$fl`; } }

    Untested, yadda, yadda.

    </snarky_mode>

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Auto-Merging textfiles
by Anonyrnous Monk (Hermit) on Dec 17, 2010 at 17:06 UTC
      Thank you both for your answers, will look into doing it one of these ways.