Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Sending open file contents through an external program

by pdac (Novice)
on Aug 21, 2018 at 21:06 UTC ( [id://1220804]=perlquestion: print w/replies, xml ) Need Help??

pdac has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I can't seem to find an answer anywhere to my problem. I hope there is one. This is what I am trying to do ...

I have a perl script using CGI to collect the contents of an uploaded file from a web user. I have an open file handle that is serving the input. What I want to do is re-direct that input through an external program and read back its output.

I've discovered how to use open to pipe to the standard input of an executed program OR to collect the standard output from one. I've also discovered socketpair() which I can see can be used with fork() to send/receive data to/from a parent and child process. But I cannot see how I can execute an external program and both send to its standard input and read from its standard output.

The only solution I can think of is to use a temporary file (either for the input or the output of the external program). But I'd prefer not to go this route. Is there a better way?

Can anyone help with this?

BTW: I only need to keep compatiblity with UNIX

Replies are listed 'Best First'.
Re: Sending open file contents through an external program
by hippo (Bishop) on Aug 21, 2018 at 21:22 UTC
      Example:
      my ($in, $out, $err) = (\*CHLD_IN, \*CHLD_OUT, \*CHLD_ERR); my $pid = open3($in, $out, $err, 'myprog -xyz blah') or die 'internal error'; print $in "some input\n"; close $in or die "$!: closing child STDIN\n"; my @out = <$out>; my @err = <$err>; close $out or die "$!: closing child STDOUT\n"; close $err or die "$!: closing child STDERR\n"; waitpid( $pid, 0 ); my $chexit = $?; print STDERR @err; chomp @err; @err and exit $chexit >> 8; # output from subprocess is in @out ...
        As it turned out, for various reasons, I had to create an intermediate file anyway. But this example is just what I was after - many thanks, I will surely have use for it sometime soon.

      I had not (didn't know about them). They certainly look interesting - I will check them out tomorrow.

      Many thanks for the steer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1220804]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-03-29 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found