Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Capturing input from pipe?

( #76108=categorized question: print w/ replies, xml ) Need Help??
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • 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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (8)
    As of 2014-09-20 02:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (151 votes), past polls