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

Hi i am dealing with an object that I've created on my own. I want to catch control-C and throw a graceful exit subroutine that I have defined within the package itself. But i'm having problems with referencing to a method of an object.
i.e This works SIG{INT} = \&gracefulExit; This doesn't work SIG{INT} = "$self->gracefulExit"; ################ package abc sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } sub initialize { my $self = shift; $SIG{INT} = "$self->gracefulExit"; } sub gracefulExit { my $self = shift; # Cleanup exit(); } 1 ################ # start of script use abc; $test = new abc(); $test->initialize(); # Control- C returns ... SIGINT handler "abc=HASH(0x229c34)->gracefulExit" not defined. I'm not sure why this is so, appreciate the help guys.

Replies are listed 'Best First'.
Re: Signal calling to object method
by Anonymous Monk on Jun 11, 2009 at 00:33 UTC
    $SIG{INT} = sub { $self->gracefulExit }
Re: Signal calling to object method
by Anonymous Monk on Mar 07, 2017 at 16:20 UTC
    Use this:
    $SIG{INT} = sub { $self->gracefulExit };
      Whoops, the prior answer didn't show up on the mobile version (nor did the timestamp for some reason). Disregard.