Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: 'better mousetrap': how to perform timed event

by BrowserUk (Pope)
on Apr 23, 2003 at 11:28 UTC ( #252519=note: print w/replies, xml ) Need Help??

in reply to 'better mousetrap': how to perform timed event

Thought I'd put the words into some code as I thought of a use for it:)

#! perl -slw use strict; use constant LOGFILENAME => 'your logfile name goes here'; use constant CHUNKSIZE => 100; # Vary this to suit your application. open my $log, '<', LOGFILENAME or die $!; # Should use the constants here, but its easier to remember the number +s than # remember the names of the constants and which module to pull to impo +rt them. seek $log, 0, 2; my($lastsize, %events) = (-s LOGFILENAME); while(1) { my $size = -s LOGFILENAME; if ($size > $lastsize + CHUNKSIZE) { # Has the file grown (enough) +? my $line = <$log>; #Read the next available line my $now = time; # Record the time print $now, ' : ', $line; # Just for testing $lastsize += length $line; # remember the new position. if ($line =~ m[ICMP]) { # Check for condition # Schedule an event of met. push @{ $events{$now + 20} }, ['This condition occured 20 seconds ago', $line]; } elsif ($line =~ m[:137]) { push @{ $events{$now + 10} }, ['This condition occured 10 seconds ago', $line]; } } # Should give accuracy to within 1 sec by your systems clock. select undef, undef, undef,0.5; my $now=time; if (exists $events{$now}) { # Check for iminant events for my $event (@{ $events{$now} }) { # Process them all print $event->[0], $/, $event->[1]; } delete $events{$now}; # Remove them when done } }

Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://252519]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2021-01-20 14:28 GMT
Find Nodes?
    Voting Booth?