For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2004 > run a function every so often *inside* the script automatically...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author run a function every so often *inside* the script automatically...
Dan - Cpanel Support

2004-04-25, 8:32 pm

Hello list!

On a sleepless Friday night/Saturday morning a question cam to mind and
I had to ask...

Assume I have a script that will run for 10 seconds.

Is it possible to have a function executed every 2 seconds?

I know it sounds weird but how would one go about something like that?

Something like:

#!/usr/bin/perl

use strict;
use warnings;

#here's where it gets fake :)
use InternalTimer;

my $icron = InternalTimer new;

my $job = $icron->job('Name',sub { print 'This gets done every 2
seconds' }, 2);
$job->start; # 1 or 0
sleep(10); # would print 'This gets done every 2 seconds' 4 or 5 times
$job->run; # execute it now
$job->error; # error since last call to error
$job->status; # 1 = still running 0 = error, undef = not started or
stopped
$job->verbose;# verbose status form start or last call to verbose
$job->amt; # how many times its run in this script
$job->stop; # 1 or 0
# end fake example code ##

any such beast?

One halfway semi practical application I could see is, say you have a
long running app that prints the date out every so often ( I realize
they could use localtime each time but hear me out...


my $now = localtime;
my $job = $icron->job('Now',sub { $now = localtime; }, 1);

print "$now\n"; # Sat Apr 24 00:57:03 2004
sleep(3); # or your long running code here ...
print "$now\n"; # Sat Apr 24 00:57:06 2004
print $job->error if !$job->status;
$job->stop;
sleep(3);
print "$now\n"; # still Sat Apr 24 00:57:06 2004 because we stopped it

Anything even remotely like what I'm describing or am I just nuts
(probably a bit of both ;p). Mostly asking for academic purposes...

TIA

Lee.M - JupiterHost.Net
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com