Contributed by Zombie lowlife
on Apr 27, 2001 at 18:25 UTC
Q&A
> input and output
Description: I want to write a script which captures input from a pipe.
e.g. user@host:/#echo "bla" | myscript.pl
I know I can't capture the input with $ARGV in this case, can
anybody tell me how I can do this?
Answer: Capturing input from pipe? contributed by tomhukins
A pipe passes the output of one command to the
input of another. So, to read the output of your
echo command, read from STDIN. An
example myscript.pl might be:
#!/usr/bin/perl
print <>;
| Answer: Capturing input from pipe? contributed by ariels Alternatively, if you really do just want to read standard input, you can read the predefined filehandle STDIN. For instance, to add line numbers to all lines coming from your pipe, you could do this:
#!/usr/local/bin/perl -w
use strict;
while (<STDIN>) {
print "$. $_"
}
But almost any program is more useful if it can read either STDIN or filenames supplied on the command line. So reading <> almost always makes more sense. | Answer: Capturing input from pipe? contributed by hdp Oft-used command line switches related to this subject are -p, -n,
and -a (especially in conjunction with -F).
See perlrun for how they work and what they do.
hdp. | Answer: Capturing input from pipe? contributed by Zombie lowlife Okeeee, that was ALLOT simpler then I thought but
it works exactly like I want, thanks allot. |
Please (register and) log in if you wish to add an answer
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, 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, 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.
|
Log In?
|
|
Chatterbox?
|
How do I use this? | Other CB clients
|
Other Users?
|
Others scrutinizing the Monastery: (1) As of 2021-02-27 01:44 GMT
|
Sections?
|
|
Information?
|
|
Find Nodes?
|
|
Leftovers?
|
|
Voting Booth?
|
No recent polls found
|
Notices?
|
|
|