Click here to get back home

every(seconds => 4)

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
every(seconds => 4) Ted Zlatanov 02-29-2008
Posted by Ted Zlatanov on February 29, 2008, 1:30 pm
Please log in for more thread options
I've extended the every() function I discussed before to handle the
'seconds' syntax, so you can say

while (<>)
{
...
if (every(seconds=>60))
{
# do this every minute when input arrives
}
elsif (every(5000) || every(943, "second one")) # distinguish the second call's
hash entry
{
}
}

Here's my implementation, which preserves the semantics of the original
every(N) function as well. I'm curious if anyone has ideas for more
things I can add. I am debating if it's worth putting on CPAN (I don't
know of anything like it already on CPAN). It seems like a really
simple piece of code that doesn't necessarily merit its own module, but
it's really useful to me...

Ted

{
my %counters;
my %time_counters;
sub every
{
my ($div, @id) = @_;

if ($div eq 'seconds')
{
$div = shift @id; # the number follows the 'seconds' string

$time_counters{caller(), $div, @id} ||= time;

my $then = $time_counters{caller(), $div, @id};
my $diff = time - ($then + $div);
return ($diff >= 0) ? ($time_counters{caller(), $div, @id} = time) : 0;
}
# else...
return !(++$counters{ caller(), $div, @id } % $div);
}
}

Similar ThreadsPosted
Converting milliseconds to seconds January 11, 2008, 3:11 am
FAQ 4.15: How can I take a string and turn it into epoch seconds? December 25, 2004, 12:03 pm
FAQ 4.15 How can I take a string and turn it into epoch seconds? February 10, 2005, 6:03 am
FAQ 4.15 How can I take a string and turn it into epoch seconds? April 25, 2005, 11:03 pm
FAQ 4.15 How can I take a string and turn it into epoch seconds? July 11, 2005, 4:03 am
FAQ 4.15 How can I take a string and turn it into epoch seconds? August 14, 2005, 10:03 am
How to run a perl script after an interval of 10 seconds October 27, 2005, 7:09 am
FAQ 4.15 How can I take a string and turn it into epoch seconds? December 4, 2005, 11:03 pm
FAQ 4.15 How can I take a string and turn it into epoch seconds? January 12, 2006, 11:03 am
FAQ 4.15 How can I take a string and turn it into epoch seconds? February 26, 2006, 6:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap