|Don't ask to ask, just ask|
Re: What is a pipe and why would I want to use one?by fokat (Deacon)
|on Jul 10, 2002 at 05:14 UTC||Need Help??|
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.
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.
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:
ls | sort
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".
Perl has the capability to use pipes. It can automatically send your program's output to a pipeline by saying something like
open(FHANDLE, "| sort")
Or get input from a pipeline, such as in
open(FHANDLE, "ls |")
You also have "low level" control of the pipes. You can create a pipe, fork() 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 pipe. See perldoc for more details and perlipc for an excellent tutorial on the different forms of IPC available on Perl.
Hope this helps.