Bash – Process Substitution with Perl Script

bashperlprocess-substitution

I have a Perl script which requires the following arguments:

count.pl OUTPUT_FILE INPUT_FILE

What I want to do is to use process substitution instead of specifying INPUT_FILE explicitly, for instance:

count.pl count.txt <(cat test.txt)

However, it seems that the script does not receive any input from process substitution.

What I'm doing wrong?

Best Answer

The script uses -T the-source-file to determine whether the file contains text.

That -T operator reads the start of the file and uses heuristics to determine whether it looks like text or not. But if the file is a pipe like in the <(...) case, then what it has read cannot be read again.

You can reproduce with:

$ perl -e '$file = shift; -T $file; open F, "<", $file; print while <F>' <(seq 1867)

1861
1862
1863
1864
1865
1866
1867

See how -T has consumed 8KiB from the output of seq which are no longer there for the while <F> loop to read.

Here, you could use zsh and its =(...) form of process substitution that uses a temporary file instead of a pipe. Or (... | psub -f) in the fish shell. Or remove that call to -T from that script. Or rewrite that script so it works with <<>>/<> (so it would also work on stdin when not passed any file argument) and outputs on stdout. I don't really see the point of it taking file names as arguments as it processes all of them in a stream fashion.

Related Question