PERL: Trapping EXIT


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL: Trapping EXIT
# 1  
Old 04-11-2005
PERL: Trapping EXIT

Hey Everyone,

Just starting with PERL (5.8.2) after years of KSH. Is there a way to trap the exit as you can in KSH (i.e., "trap EXIT_SCRIPT EXIT")?

Thanks in advance for any help,
gsatch
# 2  
Old 04-11-2005
Don't think of it so much as "trap", but as a signal handler. Perl uses a simple signal handling model: the %SIG hash contains names or references of user-installed signal handlers. These handlers will be called with an argument which is the name of the signal that triggered it.

For example, catch an interrupt and exit with a nasty message:

$SIG{INT} = sub { die "\nWhy'd you interrupt what I'm doing?\n" };

You can call functions etc.

Cheers,

Keith
# 3  
Old 04-12-2005
Thanks for the reply, Keith. The 'trap' in KSH allows a section of code to be ran everytime the script exits (the exception may be when it's killed via "kill -9"), allowing for the script to clean-up (i.e., remove temp. files, etc...) after itself. It's pretty broad where $SIG in PERL is more specific but, I could achieve the same thing if I trapped all of the appropriate signals and sent them all to the same block of code.

Thanks again Keith,
Greg
# 4  
Old 04-12-2005
FYI, if you need to trap normal exit (including die()s, not triggered by OS signals), you may be able to use an END {} block, which is best for normal cleanups.
# 5  
Old 04-19-2005
for some reason i'm not getting notified of updates to this thread so I just found this ...

cbkihong - looks to be EXACTLY what I'm looking for! I've done some simple testing and it's doing what I was hoping. Thanks for the reply.

Greg
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: trap signal 'exit': why I am not able to have it work??

First time trying to work with signals in Perl. Reviewing example I try it, but not able to get it work for 'exit'. I hope, I am correct, assuming, that the ending any code by exit $return_code; the $SIG{EXIT} should be de-referenced and processed?! So, I have such code, that, I assume,... (5 Replies)
Discussion started by: alex_5161
5 Replies

2. Shell Programming and Scripting

[Solved] Perl, Deep recursion? exit() ignored?

Hi all, I am calling a subroutine which checks if a log file is writeable and if not, prints something stdout and also log something into the same file, which doesn't work neither of course. Anyway, there is enough exit() calls, that should stop it working, but the problem is, that I get the... (5 Replies)
Discussion started by: zaxxon
5 Replies

3. Shell Programming and Scripting

How to capture the exit code of a shell script in a perl script.?

hi, i want to pop up an alert box using perl script. my requirement is. i am using a html page which calls a perl script. this perl script calls a shell script.. after the shell script ends its execution, i am using exit 0 to terminate the shell script successfully and exit 1 to terminate the... (3 Replies)
Discussion started by: Little
3 Replies

4. Shell Programming and Scripting

Perl Question Grep and exit status

Im being forced to write in perl. I prefer KSH or Expect, so I suppose its time to become more fluent with perl. I have the following problem. I want to loop through Filea and check that each line in Filea is resident in Fileb. Filea contents two four six eight houseboat Fileb... (4 Replies)
Discussion started by: sumguy
4 Replies

5. Shell Programming and Scripting

Perl | catching the letter 'Q' for exit.

Hi all, I made a simple script with a prompt menu in Perl. All working good, but I want to add an option while the program is running that on every time when the user press 'Q' the program will exit. I know I can use $SIG{'INT'} or any other %SIG option. This option is a unix signal which I... (3 Replies)
Discussion started by: RedGrinGo
3 Replies

6. Shell Programming and Scripting

Trapping exit and continuing

Hello I need to source a script. But that script terminates with a trailing exit. Which exits my script. I'm using bash, and this doesn't work: trap 'echo disabled' EXIT source other_file trap '' EXIT Instead, it calls my trap, but then exits anyway. I could get disgusting and... (4 Replies)
Discussion started by: brsett
4 Replies

7. Shell Programming and Scripting

perl how to exit a while loop and quit reading the input file

I am reading a file using While loop while <FILE> { $_ = <FILE>; process data... } I would like to quit reading the file once I encounter a String pattern. How do i do it. is it if (/SUMMARY/) { last; } I am having problems with uninitialized value in pattern... (1 Reply)
Discussion started by: subhap
1 Replies

8. Shell Programming and Scripting

exit ststus 9 from perl system command

HI all, can anyone tell me what does exit status 9 from perl's system function meant. I am using system fuction to execute a shell script as : my $s=system ('script.sh' ,arg1 ,arg2); print $s; the output is 9. Thanks in advance. !!:confused: (1 Reply)
Discussion started by: glamo_2312
1 Replies

9. Shell Programming and Scripting

How to get exit status codes in bash from Perl?

I apologize if I have already posted this query. I scanned back quite a few pages but could not find such a query. If my perl code contains "exit(33)" how can I get that value in bash for use in a "if" statement. Thanks, Siegfried (5 Replies)
Discussion started by: siegfried
5 Replies

10. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies
Login or Register to Ask a Question