I'm writing a simple program which is intended to filter
input from a pipeline. Most of the input is sent as output
untouched. Part of it is modified or used to extract
information. In its simplest form — doing nothing — the
program filter is:
$|++;
while (<>) {
print;
}
The main program sometimes outputs progress updates of its
task, overwriting the same visual content through the use of
lines ending with a carriage return. Piping such content to
filter blocks all output:
$ perl -e '$|++; print ++$a, "\r" and sleep 1 while 1' | filter
Is there an easy way to read those lines in the same loop
fashion, or should I go the sysread way? I'm looking for
something similar to what would happen if it was possible to
set the record separator to "\n or \r".
If the input isn't so large that efficiency is a concern, this version of filter is more robust
while (!eof(STDIN)) {
$_ .= getc(STDIN);
if (/[\r\n]/) {
# manipulate $_, if desired
print;
$_ = "";
}
}
print;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With