Re: IPC::Open2::open2() -- How to pass strings stdin, stdout?
- From: Jerry Krinock <jerrykrinock@xxxxxxxxx>
- Date: Tue, 8 Sep 2009 09:58:08 -0700 (PDT)
On Sep 8, 7:31 am, Ben Morrow <b...@xxxxxxxxxxxx> wrote:
...if you don't need true interaction (that is, if you know what
input you're going to send before you start) then it's often easier.
It's also likely to be *much* more portable to non-Unix systems, if
that's a concern.
Thank you, Ben. That's what I'm doing now -- writing my stdin to a
temporary file and getting stdout using backticks. Works great.
Here's a little function to help archive readers get started...
=com
Execute a given external program (command). The program must
take input from a file specified as the last parameter on
its command line. This function takes input you provide and passes
it to the program in a temporary file. The program's stdout is
returned.
Provide the command, with any command-line options, as the
first parameter, and the input data as the second parameter.
Use this in lieu of open2() and the methods of Expect.pm, both
of which will hang indefinitely unless the program supports
unbuffered I/O, typically a -u option.
=cut
sub getStdoutWithInputFromCmd {
my $cmd = shift ;
my $stdin = shift ;
# Write stdin to temp file
my $tempFilePath = File::Temp->tmpnam() ;
my $didWriteOK = open (TEMP,">$tempFilePath") ;
print TEMP $stdin ;
close (TEMP) ;
# Execute command
my $stdout = `$cmd \"$tempFilePath\"` ;
# Clean up temporary file
unlink($tempFilePath) ;
return $stdout ;
}
.
- Follow-Ups:
- Re: IPC::Open2::open2() -- How to pass strings stdin, stdout?
- From: Jerry Krinock
- Re: IPC::Open2::open2() -- How to pass strings stdin, stdout?
- References:
- IPC::Open2::open2() -- How to pass strings stdin, stdout?
- From: Jerry Krinock
- Re: IPC::Open2::open2() -- How to pass strings stdin, stdout?
- From: Uri Guttman
- Re: IPC::Open2::open2() -- How to pass strings stdin, stdout?
- From: Jerry Krinock
- Re: IPC::Open2::open2() -- How to pass strings stdin, stdout?
- From: Ben Morrow
- IPC::Open2::open2() -- How to pass strings stdin, stdout?
- Prev by Date: Re: Need expert help matching a line
- Next by Date: Re: Need expert help matching a line
- Previous by thread: Re: IPC::Open2::open2() -- How to pass strings stdin, stdout?
- Next by thread: Re: IPC::Open2::open2() -- How to pass strings stdin, stdout?
- Index(es):
Relevant Pages
|