<?xml version="1.0" encoding="windows-1252"?>
<node id="180681" title="Re: What is a pipe and why would I want to use one?" created="2002-07-10 01:14:52" updated="2005-08-12 15:17:50">
<type id="11">
note</type>
<author id="107541">
fokat</author>
<data>
<field name="doctext">
A pipe is a form of Inter-Process Communication or IPC. Think of it as an actual pipe that takes the output of a process and "pipes it" to the input of another process.&lt;P&gt;

In most operating systems that support this mechanism, the pipes are implemented as file descriptors that one of the processes writes to and the other, reads from.&lt;P&gt;

Pipes are very useful in producer - consumer problems. In Unix you'll see a lot of that, when you need to combine two commands to perform a particular action. For instance, in order to get the list of files in your current directory, sorted by lexicographical order, you would issue the following shell command:&lt;P&gt;

&lt;tt&gt;ls | sort&lt;/tt&gt;&lt;P&gt;

When handling this command, the shell will arrange for the process "ls" to run, sending its output to a pipe and the process "sort" to take its input from the same pipe. Therefore, every byte produced by the "ls" command will be eventually read and processed by "sort". &lt;P&gt;

Perl has the capability to use pipes. It can automatically send your program's output to a &lt;i&gt;pipeline&lt;/i&gt; by saying something like&lt;P&gt;

&lt;tt&gt;open(FHANDLE, "| sort")&lt;/tt&gt;&lt;P&gt;

Or get input from a pipeline, such as in&lt;P&gt;

&lt;tt&gt;open(FHANDLE, "ls |")&lt;/tt&gt;&lt;P&gt;

You also have "low level" control of the pipes. You can create a pipe, &lt;tt&gt;fork()&lt;/tt&gt; and use it to keep father and child (or siblings or whatever) happily talking to each other. The call to do this is (surprisingly) called &lt;tt&gt;pipe&lt;/tt&gt;. See &lt;tt&gt;perldoc&lt;/tt&gt; for more details and &lt;tt&gt;perlipc&lt;/tt&gt; for an excellent tutorial on the different forms of IPC available on Perl.&lt;P&gt;

Hope this helps.&lt;P&gt;</field>
<field name="root_node">
180671</field>
<field name="parent_node">
180671</field>
</data>
</node>
