Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Have a perl script launch another script or program, then write to that script's STDIN

by aitap (Deacon)
on Jan 06, 2014 at 08:01 UTC ( #1069469=note: print w/ replies, xml ) Need Help??


in reply to Have a perl script launch another script or program, then write to that script's STDIN

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.


Comment on Re: Have a perl script launch another script or program, then write to that script's STDIN
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1069469]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-11-24 02:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (135 votes), past polls