From: "Beau E. Cox" <[EMAIL PROTECTED]> > I would like to pass a string scalar to an involked process > as STDIN; I can do this with IO::File and IO::Socket > classes, but not IO::String. > <snipped> > > I get 'cannot dup: Invalid Argument' when trying IO::String as the > source. It seems 'fileno' returns undef for IO::Srting (there is a > note in the docs that fileno for IO::String returns undef for Perl < > 5.6 - I'm using 5.8.0.) > > I'm not limiting myself to IO::String - any method of assigning to > STDIN to a string scalar would be OK - I dearly want to avoid the > overhead of doing something silly like writing to a temp file.
It would be easiest to use IPC::Open2; # create the other process and a pipe between that process # and yourself $pid = open2(\*RDRFH, \*WTRFH, 'perl some-script 2>&1') or die "Can't execute the script: $!\n"; # write to the pipe print WTRFH $the_string; # close your side of the outbound pipe to signal the end of input close WTRFH; # get all the data as a single string $results = do {local $/; <RDRFH>}; # close the inbound pipe close RDRFH; # reap the "zombie". This is a Unix necessity. waitpid $pid, 0; This may be a little problematic since if the other scripts produces too much output before you are done with printing the string to it both the old and the new scripts will hang. If you need to be able to do this you should either make sure $the_string is small enough and/or that the other script doesn't print too much data before you start reading it. If you were running the scripts under a Unix you could use select() (the four parameter form) to test whether it's safe to write to or read from the pipe in a loop and print $the_string in chunks. This doesn't work on hilehandles under Windows though :-( Therefore if you have too much data it would probably be easiest to print it into a temporary file and then run $result = `perl some-script < $tempfile 2>&1`; unlink $tempfile; HTH, Jenda ===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]