[Memphis.pm] bidirectional pipe

Carl Brock Sides csides at autozone.com
Fri Oct 20 11:23:48 CDT 2000


Suppose you have a program X, and you want to write (in Perl, of
course), a wrapper W for this program. You want W to take standard
input, modify it, and pass it to X; then W should get back standard out
from X, and pass it back.

If you were writing this in Bourne shell, it would be pretty straight
forward. Let's say you want to replace all occurances of "foo" with
"bar" in standard in before passing it on to X, and then replace each
occurance of "baz" with "quux" in what comes back from X. Then your
wrapper would look something like this:

#!/bin/sh

sed s/foo/bar/g | X | sed s/baz/quux 

That is, what you seem to need is a bi-directional pipe, to and from X.

However, the way you'd think this would work in perl, doesn't. Let's say
you want to open a bi-direction pipe to and from /bin/cat. You might try
something like this:

csides at tanuki:/tmp$ cat foo.pl
#!/usr/local/perl-5.6/bin/perl -w
open(CAT, "| /bin/cat |") or die $!;
close CAT;
__END__

However, when you try to run it:

csides at tanuki:/tmp$ ./foo.pl 
Can't open bidirectional pipe at ./foo.pl line 3.

Since the obvious answer doesn't work, what's the most elegant way to do
this in perl?

-- 
Brock Sides
csides at autozone.com

The original plan [for GNOME] was to aim to make a desktop as good as the Macintosh, and we should not lower our ambition by making one merely as good as Windows. -- RMS 
----------------------------------------------------------------------------
To unsubscribe, please send email to majordomo at pm.org
with 'unsubscribe memphis-pm-list' in the body of the message.
----------------------------------------------------------------------------




More information about the Memphis-pm mailing list