Hello fellow monks, I am writing a program built around a loop on a IO::Select object. So far so good. Now I need to throw in a task that must be regularly executed. So I thought setting it as a SIGALRM handler. Sounds simple:
use strict; use warnings; use IO::Select; use IO::Socket; use POSIX qw< :signal_h >; my $port = 6681; my $listen = IO::Socket::INET->new( Proto => "tcp", LocalPort => $port, Listen => 10, Timeout => 10, ) or die "Can't bind socket to port $port: $!"; my $select = IO::Select->new($listen); print STDERR "listing on port $port\n"; my $alarm_handler = sub { print STDERR "! alarm handler\n"; alarm(2); }; my $sigset = POSIX::SigSet->new(SIGALRM); my $sa_alarm = POSIX::SigAction->new($alarm_handler, $sigset, SA_RESTA +RT); sigaction(SIGALRM, $sa_alarm); #$SIG{ALRM} = $alarm_handler; alarm(2); while (my @ready = $select->can_read) { for my $fh (@ready) { if ($fh == $listen) { my $sock = $listen->accept; $select->add($sock); } else { # ... } } }
Execpt that when running this program, it outputs:
$ perl sigalarm.pl listing on port 6681 ! alarm handler
and exits after the first SIGALRM. What is the obvious thing I'm missing to make the while restart (apart from nesting it in a while(1) { .. } loop)?

In reply to IO::Select and alarm() by Maddingue

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.