A pipe is a method of Interprocess Communication (IPC). It supports moving data from one process to another by creating a pair of filehandles, one write-only, one read-only. The pipe must be created before fork so that the filehandles are known to both processes. The writer process closes the read-only handle, the reader closes the write handle. At that point, the writer prints whatever it has to communicate to the write handle, and the reader will find the data on the read handle.

I've seen plenty of good examples here, but here is one that does so little that the main features are easy to see:

#!/usr/bin/perl -w use strict; my ($in,$out,$pid); pipe $in, $out or die $!; defined( $pid = fork() ) or die "No fork: ", $!; if ($pid == 0) { # in child close $out; print "From Child: ", $_ while <$in>; exit 0; } # in parent close $in; print $out "From Parent: ", $_ while <>; close $out; wait;
We close $out in the parent so that the child's print loop ends, and it exits. The parent waits for the child's exit so as to clean up afterwords.

There are lots of other ways of doing multiprocess programming and IPC. There is a form of the open function which takes care of the details for you.

I'd recommend Advanced Programming in the UNIX Environment for a good explanation of processes and ipc generally. perlipc is a concentrated course in using perl's process and ipc functions together.

After Compline,
Zaxo


In reply to Re: What is a pipe and why would I want to use one? by Zaxo
in thread What is a pipe and why would I want to use one? by whiteperl

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.