Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

How to call a script interactively from perl

by LetMeAsk (Initiate)
on Oct 08, 2012 at 20:26 UTC ( #997872=perlquestion: print w/replies, xml ) Need Help??
LetMeAsk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i am newbie to perl and coded a small perl program with the below objective as follows:
  • 1. Triggering another script(ThirlPartyScript.start) from this perl.
  • 2. Directing the output of that script in a file such that only those lines are stored which match a string.
  • 3. Mail the file generated at the step 2.
I am able to achieve the above objectives on a test script. But ThirlPartyScript.start after completion expect some inputs and hence user interaction, so when i call this script from my perl code, it never completes as it seems to be waiting for that user interaction to complete. below is the code i am using to execute that third party script:
#Run the command open( CMD, "$run_cmd|" ) || die "Not able to run \"$run_cmd\":$!\n"; while( <CMD> ){ # read command's output from the pipe # do write to file with what we read back from the proces print $fh_cmd_in $_; }
Can someone let me know how to supply that iterative argument when calling a script from perl so that the called script completes? Thanks

Replies are listed 'Best First'.
Re: How to call a script interactively from perl
by BrowserUk (Pope) on Oct 08, 2012 at 22:07 UTC

    If the script is a purely command line program that takes its inputs from stdin, you can avoid IPC::open3 by feeding the required interactions to via a pipe.

    Say the script requires two lines of input, like this one:

    #! perl -slw use strict; chomp( my $interact1 = <STDIN> ); chomp( my $interact2 = <STDIN> ); print "Got: $interact1 $interact2";

    Then you can feed it the required inputs and retrieve its output like this:

    C:\test>perl -E"$p = open I, '-|', '( echo fred & echo jim ) | interac'; say for <I>;" Got: fred jim

    This often will not work for programs that expect passwords.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: How to call a script interactively from perl
by zentara (Archbishop) on Oct 09, 2012 at 12:09 UTC
    Here is a simple usage of IPC::Open3 showing how to write to your STDIN.
    #!/usr/bin/perl use warnings; use strict; use IPC::Open3; #interface to "units" calculator #my $pid = open3(\*WRITE, \*READ, \*ERROR,"units -t"); #terse output my $pid = open3(\*WRITE, \*READ, 0 , "units -t"); #if \*ERROR is set to 0, STDERR is sent to STDOUT #chomp(my $prompt = <READ>); #print "$prompt\n"; foreach ( '100 yards', '1 mile', '1 kilometer', '1 parsec', '1 foobar' +){ print WRITE "$_\n meters \n"; chomp(my $answer = <READ>); print "$_ = $answer meters\n"; } __END__

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thanks all for the answers, let me detail the situation again, The third party script after completion (first round) ask a question:
      Select Option :
      Manually if i supply an "X" then it quits the script but as i am calling this third party script from my perl script i want perl to terminate that script so that the output of the thirdpartyscript can be written to a file(by perl) and so that i can use that file. Sorry if you replied to the exact scenario. Thanks
        You have me confused. How can you terminate the script in a way that allows it to output to a file? You need to describe in exact detail how this third party script runs. Maybe you are looking to put the process in the background, i.e. a background process?

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re: How to call a script interactively from perl
by Illuminatus (Curate) on Oct 08, 2012 at 21:21 UTC
    You want Open3. This assumes you know what you need to write to the third-party script to get it to exit gracefully


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997872]
Front-paged by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-01-24 01:01 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (254 votes). Check out past polls.