in reply to Exiting from capturing output in a while?

Your code as best I can make it out: (please use code tags in the future)
#!perl $program = "c:\\progdir\\bin\\proggie.exe"; $dstfile = "d:\\scripts\\log\\archive\\proggie.log"; $arg0 = "\"Blah.Blech.>\""; $timeout = 60; $starttime = time; open( PRG, "$program $arg0 |") || die "Couldn't open PRG: $!\n"; open (LF, ">$finaldest"); while (<PRG>) { print LF "$_"; $difftime = time - $starttime; last if ($difftime > $timeout); } close (LF); close (PRG);
My untested suggestion:
#!perl use strict; # ALWAYS! (Why? Helps catch typos before run time... among + other things) $|=1; # This disables buffering. JCWren's Suggestion. my $program = 'c:\progdir\bin\proggie.exe'; my $dstfile = 'd:\scripts\log\archive\proggie.log'; my $arg0 = '"Blah.Blech.>"'; my $timeout = 60; open( PRG, "$program $arg0 |") or die "Couldn't open '$program $arg0', + $!"; open(LF, ">$finaldest") or die $!; my $inloop = 0; my $endtime = time + $timeout; # Only do the math once. while( <PRG> and $endtime >= time ) # '>=' or '>', your choice. { if( not $inloop ) { # This will get run the first time, but not after. # As per JCWren's Suggestion. print STDERR "Inside Loop\n"; $inloop = 1; } chomp; print LF $_, "\n"; } close(LF) or die $!; close(PRG) or die $!;
Of course most of this is clean up, I can't see anything really wrong with your code. I don't usually use last since I don't always see it when looking for exits from a loop. Of course, last ought to work, so I tried a simple cmd liner:
C:\>perl -we "$i=0;$stop=time +2;while(1){print $i++, qq!\n!; last if +time > $stop}"
Which ran for roughly two seconds, no problem.

Replies are listed 'Best First'.
(jcwren) RE: Re: Exiting from capturing output in a while?
by jcwren (Prior) on Aug 08, 2000 at 05:38 UTC
    A couple of basic debugging tests need to be applied here:
    1. Have you proven that you're getting to the 2nd open statement?
    2. Have you proven you're actually spending time in the loop?

    It seems to me that it the program 'proggie.exe' never exits, then if open() uses a system() call internally, and not a fork(), it's waiting for the program to finish before the output will be passed back to the calling process (i.e. your script).

    In a *nix land, this might work, I don't know for sure. But in a Windows environment, I think you're going to have problems.

    I would recommend turning off buffering ($| = 1) and putting some judicious print statements to make sure you're getting as far as you think you are.

    --Chris

    e-mail jcwren