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

Dear Monks,

I've tried the following:
#! /usr/bin/perl use Config; select STDOUT ; $| = 1 ; while ( true ) { print "." ; sleep 1 ; } sub catch_zap { die "Somebody sent me a SIG$signame"; } #$SIG{INT} = 'catch_zap'; # could fail in modules $SIG{INT} = \&catch_zap; # best strategy
Without success.
Any suggestion what I'm doing wrong ?

Thanks in advance
Luca

Replies are listed 'Best First'.
Re: No effect with SIG{INT}
by borisz (Canon) on Jan 16, 2006 at 10:24 UTC
    thats easy, just install the sighandler before you start your mainloop ;-)
    sub catch_zap { die "Somebody sent me a SIG$signame"; } $SIG{INT} = 'catch_zap'; # could fail in modules select STDOUT ; $| = 1 ; while ( true ) { print "." ; sleep 1 ; }
    Boris
Re: No effect with SIG{INT}
by Anonymous Monk on Jan 16, 2006 at 12:19 UTC
    Now I have the problem that SIG{INT} = \&close() is executed at the beginning of the program. I'm using an object now, so when Ctrl-C is pressed this object has to be closed properly, like:
    #main $obj = DBConnect->new ; $SIG{INT} = \&close ; .... .... sub close { $obj->close ; exit ; }
    Any suggestions why this is wrong ?

    Luca
      your example work. And close is NOT called. I guess you wrote $SIG{INT} = \&close(); that line call close. You want $SIG{INT} = \&close.
      $SIG{INT} = \&close(); # wrong $SIG{INT} = \&close; # ok
      Boris
      To amplify on what borisz said, \&close() is a reference to the value returned by close(), while \&close is a reference to sub close (which need not have already been defined).