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

I want to run an external progam (another perl script if you must know) from my perl script and I want to give it some data in STDIN to work on. But I can't seem to ever pass the second script any STDIN characters.

Even the naive write file stdin.txt; `second.pl <stdin.txt`; doesn't seem to give the second script anything in STDIN.

Thanks in advance

Replies are listed 'Best First'.
Re: specifying STDIN for backticks
by CubicSpline (Friar) on Oct 29, 2002 at 18:46 UTC
    I sometimes need to do this when driving another application from perl that I don't actually control. Really, the right way to do this would be to modify your "second.pl" script to be smarter about the way it takes input, but that's another post.

    Try the following:

    #run the perl interpreter and open it's STDIN like a filehandle open PL, "|perl second.pl"; # whatever your loop/control structure is goes here while() { . . #print your input to the "filehandle", this has the effect #of sending your input via STDIN to the script print PL "Something"; . . } close PL;
    HTH =)

    ~CubicSpline
    "No one tosses a Dwarf!"

Re: specifying STDIN for backticks
by fruiture (Curate) on Oct 29, 2002 at 18:49 UTC

    You should not use qx// for anything that involves more than just `command options`. Playing around with STDIN and STDOUT is much more, you need the Good Old open().

    open my $pipe , '|' , $scriptname or die "..."; print $pipe "This goes to the parallel program's STDIN". ...

    `perldoc -f open` and `perldoc perlopentut` will help you.

    --
    http://fruiture.de
Re: specifying STDIN for backticks
by RMGir (Prior) on Oct 29, 2002 at 18:53 UTC
    You might also want to look into IPC::Open2 and IPC::Open3, which allow you a lot more flexibility in terms of handling stdin, stdout, and stderr (with Open3) for other programs.
    --
    Mike
Re: specifying STDIN for backticks
by dingus (Friar) on Nov 05, 2002 at 13:53 UTC
    FWIW it looks like hackery involving IO::Scalar and IO::Stringy and/or Tie::Handle::Scalar will do what I want. I will update this once I've figured out the exact hackery required

    Dingus


    Enter any 47-digit prime number to continue.