http://www.perlmonks.org?node_id=1069462

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

Basically, what the subject says:

I'm working on some automation and I need a perl script to launch another script and then write some data into that script's STDIN. I can't do this with command-line arguments, and I can't have a prepared file that I would just pipe in because I need to get some output from the second script first.

My google-fu has not been strong enough, although this seems like a pretty basic question...

  • Comment on Have a perl script launch another script or program, then write to that script's STDIN

Replies are listed 'Best First'.
Re: Have a perl script launch another script or program, then write to that script's STDIN
by aitap (Curate) on Jan 06, 2014 at 08:01 UTC
    It should be possible using IPC::Open2. Expect and IPC::Run also can help. For example, this is a "backticks on steroids" sub from one of my projects:
    use IPC::Open2 'open2'; sub backtick { my ($in, @cmd) = @_; # $in is a string to pass to STDIN, # @cmd is a command to run my $pid = open2(my $stdout, my $stdin, @cmd); # first we write to stdin print {$stdin} $in if defined $in; close $stdin; # then we read the response my $out = do { local $/; <$stdout> }; # then we wait for the program to die waitpid $pid, 0; # beware of deadlocks! die "$cmd[0] returned $? / error $!\n" if $?; return $out; }
    You'll need to be careful not to end up in situations where both your script and the script you are controlling via STDIN wait for input from each other. Buffering can also be a problem. More information: Bidirectional Communication with Another Process.
Re: Have a perl script launch another script or program, then write to that script's STDIN
by Anonymous Monk on Jan 06, 2014 at 09:29 UTC
Re: Have a perl script launch another script or program, then write to that script's STDIN
by Anonymous Monk on Jan 06, 2014 at 11:22 UTC

    If all you want is to give those daemons the password, you could write something like:

    $pass = ******; open($hndl,"|perl daemon1.pl"); open($hnd2,"|perl daemon2.pl"); print $hndl $pass; print $hnd2 $pass;
Re: Have a perl script launch another script or program, then write to that script's STDIN
by Anonymous Monk on Jan 06, 2014 at 07:54 UTC

    Can't you open multiple pipes from your starting program?

Re: Have a perl script launch another script or program, then write to that script's STDIN
by Laurent_R (Canon) on Jan 06, 2014 at 07:13 UTC
    Hmm, your question is to vague IMHO. Could you please be more specific or show the code that you already have and explain in which respect it does not fit your needs.

      The situation is this: I have several daemons that need to access some encrypted data to perform different operations on it. For security purposes, I want to enter the decryption password when I start the daemons, using:

      system('stty','-echo'); my $input=<STDIN>; system('stty','echo'); chomp($input);

      To make life easier for myself, I want to have one script that will start all the daemons so that I only have to enter the password once, after which this script will launch all the other daemons and provide them with the password. However, I don't know how to have the script give the daemons the password.

      Does my question make more sense now?