in reply to Re: Check for a new line
in thread Check for a new line

Hi, At the moment I'm just printing to the screen to test but in my loop I have done something like this:
$STAT_LINE = "$_"; push (@TOTAL_STATS, $STAT_LINE); if (@TOTAL_STATS >= $SPF) { foreach(@TOTAL_STATS) {print "$_"}; splice @TOTAL_STATS; }

But when there is no new line to print, the program just keeps waiting for a new line. What I want is a way to say that if there is no new line after 10 seconds push "Error" to the array

Replies are listed 'Best First'.
Re^3: Check for a new line
by jellisii2 (Hermit) on Jan 27, 2014 at 12:46 UTC
    I don't see where this changes, assuming you have the time thing sorted out. If you don't, you could try something like
    my $start_time = time(); my @data; while (time() < $start_time + 10) { # try to populate @data here } if (!@data) { print "error!\n"; } else { print @data; }

    Unless I'm misunderstanding the problem, which is entirely possible.

      Hi,

      The problem I have is that my open() command executes a program on the server(program A) that get stats from another program on the server(program B). When 'B' is running 'A' prints out a line every 10 seconds like this:

      TIMESTAMP - value,value,value,value.

      But if 'B' dies 'A' just hangs waiting for 'B' to come back up and doesn't print a new line until it can get stats from 'B'.

      So my while loop is still running and I want a way to log "Error" if no more new lines are returned every 10 seconds.

        My initial thoughts are that "program A" might be the best place to be adding this error checking and logging. Not sure if modifying "program A" is an option.

        Another route would be to have the Perl script check periodically to see if "program B" is still running. Of course, this method might not be able to distinguish between a) up and running properly and b) running but not responding.

        The only other way that I can think of is to have the Perl script periodically check on the file that "program A" is writing to. You could use something like stat to check for the last modified time stamp. I haven't use this module, but it looks File::Monitor might be another method for checking to see if the file has changed.

        Perhaps more knowledgable and experienced monks can come up with better solutions/ideas. Thought I'd toss out these ideas in case one of them might help you out.

        I'd have to sit and think about it more than I have time to, but threads or one of its cousins may be useful in this instance. Have the program run in a separate thread, dumping data to somewhere you can grab it, and tail that file every 10 seconds.