I'm not going to say much about children because the previous posts provide quite alot of info. To understand pipes you need to know a bit about the unix programming philosopy. A typical unix distribution comes with a lot of small programs that do one thing really well. For example, cat streams the contents of a file, grep matches patterns, less allows you to browse text, gzip compresses files, etc. Pipes allow you to "pipe" the output of one program to the input of another in a rather transparent way. Each program's input and output is always being piped somewhere. The shell pipes your keyboard input to the program you are currently running, and captures your program's output to send to the terminal. You can think of unix as a system of little black boxes that all do one thing. There are a bunch of pipes lying around that can be connected between the black boxes however you wish. By rewiring the pipes, you can create a multitude of complex behaviors out of a collection of simple machines.

You can solve a lot of programming problems on a unix system simply by piping a bunch of small programs together. For example, if I wanted to find all the connections to my web server from ip 216.122.66.112 I might write this perl script:

open IN, "<access.log" or die; while (<IN>) { print if $_ =~ /^216\.122\.66\.112/; } close IN;
But say I want to be able to browse these results in less. I'd have to write my output to a file and then call less on that file. Or I could do this in the shell:
% grep '^216\.122\.66\.112' access.log | less -S
Or I could pipe the output of my perl program to less. Rewriting less in perl would be a real pain, but any program can allow the user to browse its output with less with the help of a pipe. It's very nice to be able to pipe output from another process into your perl program so you don't have to continually reinvent the wheel.

If you want to learn more about this sort of thing, I'd suggest picking up a good general unix reference as a first step. For example, W. Richard Stevens has written a number of great unix books, covering topics from interprocess communication to network programming. Once you have a handle on unix interprocess communication, chapter 16 of the Perl Cookbook will provide you with most of what you need to know about the how in Perl.


In reply to Re: Pipes? Child Processes? by dbp
in thread Pipes? Child Processes? by arrow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &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.