jaco has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks
I'm trying to create a script that uses an alarm handler to cause a reoccuring event within a while loop.
To the best of my knowledge the manner at which I'm doing this is acceptable. However, I'm curious
to know if there's a better way. Or perhaps a more proper or stable way. Not that I knowingly find this
unstable, but the line in perlipc
That means that doing nearly anything in your handler could in theory trigger a memory fault and
subsequent core dump.
has always been a concern. Anyway, it's something like ...
#!/usr/bin/perl $SIG{ALRM} = sub {alarm(0);print "blah\n";alarm(10);}; open(FILE, "tail -f /var/log/messages|"); $| = 1; alarm(10); while(<FILE>){ print $_; }

Replies are listed 'Best First'.
Re: Reoccuring time based event inside a loop (threads)
by BrowserUk (Patriarch) on Oct 06, 2004 at 17:43 UTC

    It's easy and safe with threads.

    #! perl -slw use strict; use threads qw[ async ]; use threads::shared; $| = 1; ## Simulate tail open FILE, q[ perl -le"$|=1; print and select(undef,undef,undef,.1) for 1 .. 30" + | ] or die $!; my $done : shared = 0; my $t = async { while( !$done and sleep 1 ) { print 'Blah'; } }; while( <FILE> ) { printf; } $done = 1; $t->join; __END__ P:\test>397046 1 2 3 4 5 6 7 8 9 10 Blah 11 12 13 14 15 16 17 18 19 Blah 20 21 22 23 24 25 26 27 28 Blah 29 30 Blah P:\test>

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: Reoccuring time based event inside a loop
by Joost (Canon) on Oct 06, 2004 at 16:19 UTC