tags:

views:

692

answers:

2

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.

+6  A: 

See perldoc sigtrap.

Daniel Cassidy
thanks will look up the doc for this. although was getting a bit confused with the same... could you give an example for how this can be handled?
gagneet
+10  A: 

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.

Kevin Beck
thanks, that was helpful.
gagneet
does %SIG require any standard library to be used?
gagneet
No, it does not.
Aristotle Pagaltzis