Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

finding the status of the program being called from perl

by roseberry123 (Acolyte)
on Jun 10, 2004 at 03:45 UTC ( #362970=perlquestion: print w/ replies, xml ) Need Help??
roseberry123 has asked for the wisdom of the Perl Monks concerning the following question:

hi,

i am calling an interactive program from perl using open2(). is there a way to know if that interactive program is waiting for input or if it is currently executing?

thanks.
rb123

Comment on finding the status of the program being called from perl
Download Code
Re: finding the status of the program being called from perl
by PodMaster (Abbot) on Jun 10, 2004 at 05:08 UTC
    is there a way to know if that interactive program is waiting for input or if it is currently executing?
    If the program is waiting for input then it must be "currently executing". Are you trying not to start more than one instance of a program?

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Oh, yeah you're right. I said that wrong. What I meant was, how do I know if that interactive program (being called from a perl script) is waiting for user input or currently processing some other commands that require no user input but only requires the user to wait.

      Does that make any sense? i'm not very articulate, am i?

      If it is of any help, that interactive program is ClustalW (an alignment program), which requires several user input (from a character user interface), before it will finally produce the final output in file form. However, in the middle of the program, it requires the user to wait for some processing before allowing the user to type some more input at the prompt.

      My current code:

      my $infile = "homologene1.fa"; my $pid = open2(*Reader, *Writer, "clustalw") or die "Cant open clusta +lw: $!\n"; Writer->autoflush(); Reader->autoflush(); STDIN->autoflush(); print Writer "1\n"; print Writer "$infile\n"; print Writer "2\n"; print Writer "9\n"; print Writer "4\n"; print Writer "\n"; print Writer "\n"; print Writer "1\n"; print Writer "\n"; print Writer "\n"; print Writer "\n"; # <----- i need to wait here before making more user input print Writer "x\n"; print Writer "\n"; print Writer "x\n"; close(Writer); close(Reader); waitpid($pid, 0);

      you can probably tell i'm very inexperienced.

      thanks for reading.

Re: finding the status of the program being called from perl
by matija (Priest) on Jun 10, 2004 at 05:25 UTC
    You could run select on the filehandle you use to communicate with the interactive program. If it returns before the timeout, and marks the filehandle as ready for input, then the program is waiting for you to send it data. If it doesn't, it isn't.

      thanks. i think this is what i am looking for. but i'm getting really confused. besides perldoc.com, do you know where i can find more information about whether i should choose $sel->can_read or $sel->can_write? i think my perl script is becoming really confused, because what used to work is now not working anymore. and also, if i am using open2(*Reader, *Writer, $cmd), which filehandle, Reader or Writer, should i use to find out whether the interactive program is waiting for input? or do i use STDIN?

      thanks very much. i really appreciate it.

        You should choose $sel->can_read for the file handle you are reading from (i.e. the filehandle that the other process is writing to).

        The filehandle you should test to see if the other program is waiting to read is the filehandle you write to.

Try proc::reliable
by andyf (Pilgrim) on Jun 10, 2004 at 09:04 UTC
    this is what I use, it may be of help.
Re: finding the status of the program being called from perl
by exussum0 (Vicar) on Jun 10, 2004 at 10:35 UTC
    My personal favourite is manipulating $0.

    At random parts in my program where the state, according to the OS, is running (whcih is correct), I like to tell what it's doing to the layman at work.

    my $origName = $0; ... $0 = "$origName: Processing file $n."; ... $0 = "$origName: Sleeping for $y seconds."; ... $0 = "$origName: Eating some cheese.";

    Bart: God, Schmod. I want my monkey-man.

Re: finding the status of the program being called from perl
by ctilmes (Priest) on Jun 10, 2004 at 12:26 UTC
    You also might want to check out Expect.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://362970]
Approved by broquaint
Front-paged by matija
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (18)
As of 2014-07-23 14:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (144 votes), past polls