Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by Zaxo (Archbishop)
on Jul 10, 2002 at 05:27 UTC ( [id://180683]=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 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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2025-01-16 15:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (54 votes). Check out past polls.