How to open a pipe for reading and writing?

W

Winston Smith

Hello,

I would like to know if there is a way to open a pipie for both reading
and writing ? What I'm trying to do is something like:

open(FILTRE, '| myprogram |');
while (<>) {
print FILTRE, $_;
my $result = <FILTRE>;
print STDOUT, $result;
}

The point is I need to give lines one by one to my program and read the
output in the same time (as the output for a given line can change the
following input).

If someone as any idea, thank you in advance.

Winston
 
B

Brian McCauley

Winston Smith wrote:

[ FAQ: "How can I open a pipe both to and from a command?" ]
thank you in advance.

If you are going to do anything in advance, it should be to read the FAQ.
 
R

Richard Gration

Hello,

I would like to know if there is a way to open a pipe for both reading
and writing ?
If someone as any idea, thank you in advance.

Use IPC::Open2.

It's quite straightforward, there's good example code in the docs for it.

HTH
Rich
 
J

Jeff Stampes

Winston said:
I would like to know if there is a way to open a pipie for both reading
and writing ?

Found in /usr/lib/perl5/5.8.3/pod/perlfaq8.pod
How can I open a pipe both to and from a command?

The IPC::Open2 module (part of the standard perl distribution) is a easy-to-
use approach that internally uses pipe(), fork(), and exec() to do the job.
Make sure you read the deadlock warnings in its documentation, though (see
IPC::Open2). See "Bidirectional Communication with Another Process" in per-
lipc and "Bidirectional Communication with Yourself" in perlipc

You may also use the IPC::Open3 module (part of the standard perl distribu-
tion), but be warned that it has a different order of arguments from
IPC::Open2 (see IPC::Open3).
 
D

David K. Wall

Winston Smith said:
I would like to know if there is a way to open a pipie for both
reading and writing ? What I'm trying to do is something like:

open(FILTRE, '| myprogram |');

I'd read the docs for open() and see where else that led me.
 
D

David K. Wall

Brian McCauley said:
Winston Smith wrote:

[ FAQ: "How can I open a pipe both to and from a command?" ]
thank you in advance.

If you are going to do anything in advance, it should be to read
the FAQ.

I think I need to reread the FAQ myself: a routine brain maintenance
task I've deferred for too long.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top