Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: What is a pipe and why would I want to use one?

by fokat (Deacon)
on Jul 10, 2002 at 05:14 UTC ( [id://180681]=note: print w/replies, xml ) Need Help??


in reply to What is a pipe and why would I want to use one?

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.

Replies are listed 'Best First'.
Re: Re: What is a pipe and why would I want to use one?
by rjray (Chaplain) on Jul 10, 2002 at 09:02 UTC

    Excellent explanation of the concepts, well worth a ++.

    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.

    I would also recommend having people look at the IO::Pipe class that is a part of the core. It's very easy to use, and integrates nicely with IO::File, IO::Socket, et al.

    --rjray

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://180681]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-28 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found