tags:

views:

260

answers:

5

I am mulling over a web-app in Perl that will allow users to create bug monitors. So essentially each "bug watch" will be a bug ID that will be passed to a sub routine along with the "sleep" time, and once the the "sleep time" is over it must recur without blocking the parent process or the peer processes.

  • I tried Schedule::Cron. It supports cron-like format but here the arguments to the subs must be simple scalars hence I ruled it out.

  • POE/Coro seem to be another options but I don't have much idea about it/ :(

Any insights ? TIA

-Matt.

A: 

Try Time::HiRes

Aaron Digulla
+5  A: 

What's wrong with Schedule::Cron? You can make any subroutine reference that you like, so you can make closures that refer to the extra or specific data you need. You don't have to rely on the argument list. Was there something else about the module that didn't work for you?

brian d foy
A: 

@(brian d foy): The reasons why i think Schedule::Cron is good for me 1: $cron->add_entry doesn't seem to provide me an option to pass @arrays/$vars to the subs.

$cron->add_entry("$temp",{'subroutine' => \&test1,'arguments' => \@array}); is not allowed.

2: I am not sure if there is a way to add new cron entry after cron->run(detach=>1); has been fired without restarting the script..

The documentation seems to disagree. I think you are doing something wrong.
Leon Timmermans
+1  A: 

If u do decide to look into Coro then it might be worth having a look at Continuity as this is a web library/framework built around Coro.

Also take a look at Squatting web microframework which "squats" on top of Continuity by default. The Squatting distro comes with some examples of using Coro::Event.

/I3az/

draegtun
+3  A: 

I tried Schedule::Cron. It supports cron-like format but here the arguments to the subs must be simple scalars hence I ruled it out.

The Schedule::Cron documentation says that arguments is a Reference to array containing arguments to be used when calling the subroutine. Pass a reference to a named array of your arguments, if you wish. Since the cron entry holds a reference to @data you can add or remove @data elements in your code as needed.

$cron->add_entry(
    '* * * * *',
    subroutine => \&mysub,
    arguments => \@data,
);

You can also use a closure, as Brian suggested:

my $var = 42;
my @arr = get_stuff();

$cron->add_entry(
    '* * * * *',
    sub { mysub($var, @arr) },
);

See the perlref man page for more information on closures.

converter42