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

Hello Guys; The situation I am having is; I wrote a script to run a file using a program installed on the system. Now, the whole thing works properly, except I have multiple files that I need the program to run on. To do his I kept the whole thing in a loop, in which the variable of the loop corresponds to the name of the file. So by thought, I expected perl to run the loop, open the file using that program and when it's done, it simply goes to the end of the loop and back to the begining and increments, then do it again. Eg. for ($x=2; $x >= 0;$x--) { for ($y=2; $y >= 0;$y--) { system("prog name -f file_$x_$y.ext >tracker_$x$y.log"); } } but instead (and rather unexpected), it stops after the first loop, and then when I check its status, I notice the program has just stopped. It's finished what it has to do but it remains within that program, and perl just stays still (I'm guessing waiting for the program to exit). Now, to solve this I always have to physically press cntrl - C in the bash (terminal), and this ends the program (not the script, but the program initiated by the script), and then the loop resumes, only to happen again. That's why I was hoping I could make perl issue the 'cntrl-c; command and then the loop continues making the whole thing autonomous. Now, if I make use of the kill, realised it would kill everything (when I just want to kil the running program (not the script). I hope I was explanatory enough.

Replies are listed 'Best First'.
Re: Perl quiting a progarm
by ikegami (Patriarch) on Jan 21, 2009 at 02:03 UTC

    Sending SIGINT can be done using kill INT => $pid. There are also ways to run the child asynchronously. One question would remain: When would it be appropriate to send SIGINT?

    Just guessing, but a simpler and more effective solution might be to signal EOF to the child's STDIN.

    system("prog name -f file_$x_$y.ext < /dev/null > tracker_$x$y.log");

    Or maybe even

    system("prog name -f file_$x_$y.ext <&- > tracker_$x$y.log");

    (I'm assuming unix from your bash reference, but I suppose you could be using cygwin.)

Re: Perl quiting a progarm
by jwkrahn (Abbot) on Jan 21, 2009 at 04:32 UTC

    You could try to run it using open instead of system:

    for my $x ( reverse 0 .. 2 ) { for my $y ( reverse 0 .. 2 ) { open my $pipe, '-|', 'prog name', '-f', "file_${x}_$y.ext" or die "Cannot open pipe from 'prog name': $!"; open my $out, '>', "tracker_$x$y.log" or die "Cannot open 'tracker_$x$y.log': $!"; print $out $_ while <$pipe>; close $pipe or warn $! ? "Error closing 'prog name' pipe: $!" : "Exit status $? from 'prog name'"; } }

    Note that in your original example the string "file_$x_$y.ext" is using the variable $x_ so you have to enclose the variable name in braces as in my example above.