2

I feel as though there should be a simple way to do this, but searching around gives me no good leads. I just want to open() a pipe to an application, write some data to it, and have the output of the subprocess sent to the STDOUT of the calling script.

open(my $foo, '|-', '/path/to/foo');
print $foo 'input'; # Should behave equivalently to "print 'output'"
close($foo);

Is there a simple way to do this, or have I hit upon one of the many "can't get there from here" moments in Perl?

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166

1 Answers1

5

The subprocess will inherit STDOUT automatically. This works for me:

open(my $f, "|-", "cat");
print $f "hi\n";

If you are not really closing the pipe immediately the problem might be on the other end: STDOUT is line-buffered by default, so you see print "hello world\n" immediately. The pipe to your subprocess will be block-buffered by default, so you may actually be waiting for the data from your perl script to reach the other program:

open(my $f, "|-", "cat");
print $f "hi\n";
sleep(10);
close($f); # or exit
# now output appears

Try adding select $f; $| = 1 (or I think the more modern way is $f->autoflush(1))

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
  • Huh. Must be a problem with the particular application, then. – Jon Purdy Nov 06 '10 at 19:11
  • Added another idea -- not sure if edits show up as notifications for you. – Ben Jackson Nov 06 '10 at 19:28
  • It doesn’t do any good to sleep after the print down a pipe: even perlio uses stdio-style pipe buffering. `select((select($f),$|=1)[0])` fixes that. Hm, is there some reason for such an exotic pipe open yet without error checking? Why not just: `open(my $f, "|cat") || die "can't open pipe to cat: $!";`? – tchrist Nov 07 '10 at 01:15
  • @tchrist: Agree some error handling is a good idea, perhaps the original problem is that the open fails? Not sure what you mean about sleep: I was just illustrating how expectations of line-buffering (stdio auto flush on newline) are broken if you open your own pipe or file and get block-buffering by default. – Ben Jackson Nov 07 '10 at 05:26
  • anybody who thinks stdio line buffers is too used to output to terminals instead of to pipes, sockets, or files. – tchrist Nov 07 '10 at 14:16