Re: IPC::Open2::open2() -- How to pass strings stdin, stdout?



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 ;
}

.



Relevant Pages

  • Mounting a temporary filesystem
    ... I'm trying to mount a certain part of my filesystem as a temporary file ... I have a incoming mail queue directory that I wanted to mount as a temp ... I flipped through the man pages and came up with this command: ...
    (freebsd-questions)
  • Re: Unix Command and Filename Rename Help Needed C++ Language
    ... > I first sent a command to Unix and sent the result to a temp. ... This is rather inefficient and not a little insecure, ... temporary file. ...
    (comp.unix.questions)
  • Re: Question on using /tmp
    ... >temporary file and my code is going to read something out of it. ... Only when I call the command in the form ... But I have to use tmpnam since ... It depends on what kind of security you are trying to implement, ...
    (comp.os.linux.development.apps)
  • Re: unresolved external error
    ... Command Lines Creating temporary file ... Build Log Rebuild started: Project: SimAPI, Configuration: Release|Win32 ...
    (microsoft.public.vc.mfc)
  • Re: noobish dsadd computer script question
    ... I do not understand how the Active Directory Command ... Line Interface accepts information from a file using STDIN. ... Judging from the text in the book and the text in the DSADD help file, ...
    (microsoft.public.windows.server.scripting)