in reply to Exiting from capturing output in a while?
My untested suggestion:#!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);
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:#!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 $!;
Which ran for roughly two seconds, no problem.C:\>perl -we "$i=0;$stop=time +2;while(1){print $i++, qq!\n!; last if +time > $stop}"
|
|---|
| 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 |