Often, I have found myself wishing that there were some way to force a command that requires a filename argument to accept input piped in from something else instead. Today was one of those days and I could not sit back any longer and take it. So, I wrote this quick little script that solves my problem.
filepipe.pl takes it's first argument as an alias that will
act as a place-holder that you place into the command where it would normally want a filename, the rest of the args afterwards are treated as the command and it's arguments.
eg:
./foo | ./filepipe.pl ALIAS ./bar ALIAS
would cause bar to accept the output of foo as if it were a file.
This works by supplying filepipe.pl's STDIN sysmlink in /proc/$$/fd as the file for bar to open, thus causing bar to
consume the data piped into filepipe.pl as the file.
This of course has it's limitations, it will only work if bar opens the file only for reading and doesn't check to see if it's a "normal" file.
Improvements (or a better method altogether) welcome:)
Note: A more portable way of doing this would be to use named pipes. But because opening a pipe for writing blocks until another process opens it for reading, this gets ugly.
You would have to fork and handle the reader and writer seperately. I am simply too lazy to do that when this does what I want with so little code:P
#!/usr/bin/perl
# filepipe.pl
my $fhalias = shift;
my $fh = fileno(STDIN);
my $file = "/proc/$$/fd/$fh";
system(
map {
s/$fhalias/$file/g;
$_;
} @ARGV
);
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.