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

Why is signal handling so ugly? Surely there's a good reason? Is there a better way to do this? For instance, why do I have to define a SigSet object that I never do anything with?
my $sigset = POSIX::SigSet->new(); my $restart_action = POSIX::SigAction->new('restart',$sigset,POSIX::SA +_NODEFER()); my $end_action = POSIX::SigAction->new('end',$sigset,POSIX::SA_NODEFER +()); POSIX::sigaction(POSIX::SIGQUIT(), $end_action); POSIX::sigaction(POSIX::SIGHUP(), $restart_action); POSIX::sigaction(POSIX::SIGPIPE(), $restart_action); POSIX::sigaction(POSIX::SIGTERM(), $end_action); POSIX::sigaction(POSIX::SIGINT(), $restart_action); POSIX::sigaction(POSIX::SIGUSR1(), $end_action);

Replies are listed 'Best First'.
Re: Setting signal handlers
by Hue-Bond (Priest) on May 09, 2006 at 20:55 UTC

    That's the POSIX way to do things. It's as ugly in Perl as it's in C, since in C it's also done a la POSIX. Fortunately in Perl we have Another Way To Do It:

    sub interrupt { print "quitting!\n"; } $SIG{'INT'} = 'interrupt'; sleep 20; __END__ ^C quitting!

    David Serrano

      Thanks David but

      I tried that first and it didn't work. Supposedly it's because "POSIX unmasks the sigprocmask properly".

      Not sure what that means, but the POSIX way works and the Perl way doesn't.

        What did you try exactly? Without some code it's difficult to guess why it's not working for you. You should post a small complete program that shows the problem and tell us what do you expect and what is happening instead.

        David Serrano

        Well, back in 2000, I gave up on a module (Server::FastPL, now on backpan) because of lost signals and unix sockets constant lock up... My experience told me to not trust signals for anything really important... I did use the $SIG way... maybe the problem is related...

Re: Setting signal handlers
by jesuashok (Curate) on May 10, 2006 at 05:52 UTC

    > why do I have to define a SigSet object that I never do anything with? Here it goes.

    Create a new SigSet object. This object will be destroyed automatically when it is no longer needed. Arguments may be supplied to initialize the set.
    Create an empty set.

    $sigset = POSIX::SigSet->new;
    Add a signal to a SigSet object.
    $sigset->addset( &POSIX::SIGUSR2 );
    For more information on SigSet

    "Keep pouring your ideas"