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

Hi, I'm trying to convert a portion of a korn-shell script for use in perl, and came across an unexpected problem. In the script is a like so:
x=`sql $pwd <<endl | grep (rest of command)
select 'something';
exit;
endl`
As far as I can tell, this sends the "select" and "exit" commands as the standard input to the spawned sql process. Try as I might, though, I can't see how to do something similar in Perl!

Put simply, I need to find a way to send a text string as the standard input to a spawned process, and read back the text generated by the process. Any help gratefully received!
  • Comment on Use text string as stdin for system process

Replies are listed 'Best First'.
Re: Use text string as stdin for system process
by jettero (Monsignor) on Jan 05, 2007 at 15:55 UTC

    I suggest you try something like DBI to perform queries in perl. It's going to be a lot easier to maintain and operate.

    But you can probably get this to work with pipes or something:

    open my $out, "|sql>tmpfile" or die $!; print $out "select 'soemthing';\n"; print $out "exit;"; close $out; my $x = `cat tmpfile`;

    To do the whole thing with pipes, look at IPC::Open3 or POE::Wheel or something.

    -Paul

      Yeah, that's pretty much what I wanted to do. But for some stupid reason, that's no-one can clearly explain to me, I'm being told I can't access their database except through the command-line tool. Don't ask - I've already given up asking :)

      Open2 and Open3 appear to be coming up as good possibilities. I'll have to go and have a look. Thanks!

        Uhhh ... how are they going to know?

        -derby
Re: Use text string as stdin for system process
by kyle (Abbot) on Jan 05, 2007 at 15:56 UTC