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

I want to handle INT Signal in My Perl Script. The Structure of script is that when a script is called it calls another Perl Package where most of the processing is done. I am using a little bit OOPS Concept where I am creating an object of another PM package. The Problem I am facing is I need to give some command line arguments to the Subroutine Handled by $SIG{'INT'} = 'Interrupt'. How can we do that? What I have done is declare $SIG{'INT') = 'Interrupt' while initializing the Object and then I call the Subroutines using that object. Now when ever an interrupt is received I want to close all file handles which are stored in $self.

Replies are listed 'Best First'.
Re: Signal Handling
by lidden (Curate) on May 13, 2012 at 08:59 UTC
    You can set set the handler to a subroutine.

    $SIG{INT} = sub { <code goes here> };

    See perldoc perlipc.

      Is there any other work out. Actually I want to write a Interrupt Subroutine which should take Parameters. This way it's not going to work.
        Well, often what you want to do is something like:

        $SIG{INT} = sub { our $sigint_gotten = 1;}; # Later in the code if(our $sigint_gotten){ our $sigint_gotten = 0; deal_with_it($arg1, $arg2, ...); }

        I can't imagine how you would send a SIGINT with a parameter but you can do whatever you want in the subroutine you assign to SIGINT. Just remember that you want to return control to the system ASAP once you're in the handler. Maybe something like this would work for you...

        $SIG{INT} = sub { my $obj = Whatever->new(); process_interrupt($obj) };