Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using alarm

11 views
Skip to first unread message

Unknown User

unread,
Oct 26, 2012, 10:46:44 AM10/26/12
to beginners
I have code that goes something like this:


my $start = time();
my $delay = 60;
...

while (my $line = <$fh>) {
my $err;
...
...
my $timenow = time();
if ( $timenow - $start >= $delay ) {
$start = $t;
dumpstats($err);
$err = {};
}
}
...

I wonder if it would be possible to replace this loop with a handler
based on alarm.
If it is possible, which would be more efficient?

Thanks

Unknown User

unread,
Oct 26, 2012, 10:47:48 AM10/26/12
to beginners
On Fri, Oct 26, 2012 at 8:16 PM, Unknown User
<knowsupe...@gmail.com> wrote:
> I have code that goes something like this:
>
>
> my $start = time();
> my $delay = 60;
> ...
>
> while (my $line = <$fh>) {
> my $err;
> ...
> ...
> my $timenow = time();
> if ( $timenow - $start >= $delay ) {
> $start = $t;
err, that would be:
$start = $timenow;

Rob Coops

unread,
Oct 26, 2012, 11:02:39 AM10/26/12
to beginners
> --
> To unsubscribe, e-mail: beginners-...@perl.org
> For additional commands, e-mail: beginne...@perl.org
> http://learn.perl.org/
>
>
>
Possible most certainly, desirable maybe...

Here is the main thing your loop can run for an x number of seconds 60 at a
minimum or 120 maybe even 1200000 as your code might not get to the end of
the loop before then. Using alarm or any type of module based on this
function will ensure that you will perform the same action a lot closer to
your desired 60 second mark. Using Time::HiRes will allow you to specify
less then seconds which might be interesting in some cases (then you will
use the ualarm call instead of allarm)

Hope that helps a little,

Rob
0 new messages