Hope I understand what you said. I did something on windows, as I don't have unix around today, sweet home, but you can easily move this to unix.
I tested the following code with:
type a.pl|perl -w a.pl
and it demonstrates how you can make your scripts to follow the behavior of tranditional pipe, and take output from another process as input.
a.pl:
@in = <STDIN>;
foreach (@in) {
print "from perl script: ", $_;
}
Update 2: I don't know whether you have control towards cmd1, cmd2, etc. or not. If yes, I would strongly suggest you to use pipe(), to help you connect those handlers into pairs.
Update: Hm...Now I know what you mean, so you want your script to act as the "middle man". Anyway, I still would like to leave my original reply there, as it is still a good answer to "the question I thought it was".
Your approach ALONE sounds good to me, but that while loop can be simplified, and you may borrow the concept from following code:
open(DATA, "<", "ex902.pl");
@in = <DATA>;#read in as array
close(DATA);
open(DATA1, ">", "data.txt");
print DATA1 @in;#flush the whole array out
close(DATA1);
However I am wondering why you are trying to do that. It sounds to me, like you are reinventing something the OS is doing for you. For the project you are working on, is there an alternative solution, which allows you to utilize your OS as much as possible? Well, any way, I believe that you must have a good reason to do this.
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.