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.

In reply to Re: Exiting from capturing output in a while? by Adam
in thread Exiting from capturing output in a while? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.