gagneet
gagneet

Reputation: 37259

How can I catch and handle a signal in Perl?

Is there any way to trap an error and exit gracefully from Perl? I am working on a script which might fail due to a SIG event from the OS or other applications running on my server. I wish to trap this event, display the error and exit after closing all files and other attributes I have open during the execution of the script.

Upvotes: 2

Views: 2803

Answers (2)

Kevin Beck
Kevin Beck

Reputation: 2390

Use the %SIG hash to install signal handlers. Example:

$SIG{INT} = 'SigIntHandler';

Where SigIntHandler is a sub that you write to be called when an interrupt is caught.

Upvotes: 11

Daniel Cassidy
Daniel Cassidy

Reputation: 25597

See perldoc sigtrap.

Upvotes: 6

Related Questions