If I want to execute a program and read the output which is stored in $output, I run it like this:
my $output = `command <<EOF;
yes
EOF`;
# print output from `command` after giving it 'yes' as input
print $output . "\n";
If I want to write to a command using a filehandle and not see the resulting output I do this:
open(COMMAND, '| command') or die("Can't run command: $!\n");
# give program 'yes' as input
print COMMAND 'yes';
But what do I do if I want to write to the command as easily as using a file handle and read whatever comes back as easily as a scalar? Essentially I want to do both of the examples above at the same time.
So I'd like to know how to:
1. Run the command
2. See what comes back and read it from $output
3. Make some decisions and print back to filehandle COMMAND
4. Make some more decisoins
5. Print back to COMMAND
6. etc...
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.