G'day aquercus,
Welcome to the monastery.
I don't have perl running on any MSWin OSes; however, here's how I might have tackled this on a *nix OS.
There may be sufficient here for you to adapt it to your OS.
I've added some notes after the code as well as a sample run.
Here's the code (pm_test_tail_pipe.pl):
#!/usr/bin/env perl
use strict;
use warnings;
use autodie qw{:all};
my $tail_cmd = 'tail -f ./pm_test_file_to_tail';
my @tail_captures;
print '*** Start reading from $tail_pipe at ', time, "\n";
my $tail_pid = open my $tail_pipe, '-|', $tail_cmd;
{
local $SIG{INT} = sub {
kill INT => $tail_pid if kill 0 => $tail_pid;
waitpid $tail_pid => 0;
};
while (<$tail_pipe>) {
push @tail_captures, $_;
}
}
print '*** Stop reading from $tail_pipe at ', time, "\n";
# Possibly process data captured, e.g. for demo only
$_ = uc for @tail_captures;
print '*** Captured via $tail_pipe:', "\n";
print for @tail_captures;
Notes:
-
I haven't included the pipe (|) as part of the tail command. Instead, I've used '-|' in the 3-argument form of open and captured the PID of the tail process.
-
local $SIG{INT} = ... traps interrupts (Ctrl-C on *nix) to stop tailing without killing the script. This only takes effect within the anonymous block so it only affects the while loop; outside of this block, interrupts act normally, i.e. Ctrl-C will terminate the script.
-
perlport may be of use with respect to kill and waitpid.
-
perlipc has information on Signals.
Sample run:
| File to Tail | Tailing Process |
|
Assume some log file that already has some data
$ cat > pm_test_file_to_tail
line 1
line 2
line 3
[terminated with Ctrl-D]
|
|
|
External program starts appending data
$ cat >> pm_test_file_to_tail
line 4
line 5
|
|
|
|
Start tailing the file
$ pm_test_tail_pipe.pl
*** Start reading from $tail_pipe at 1371707519
|
|
External program appends more data
line 6
line 7
|
|
|
|
[terminate tailing with Ctrl-C]
^C*** Stop reading from $tail_pipe at 1371707542
*** Captured via $tail_pipe:
LINE 1
LINE 2
LINE 3
LINE 4
LINE 5
LINE 6
LINE 7
|
|
External program still appending data
line 8
line 9
[External program terminated with Ctrl-D]
|
|
|
Check final contents of tailed file
$ cat pm_test_file_to_tail
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
|
|
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.