Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

How to call on terminate function in perl

Question posted by: cnsabar (Member) on May 2nd, 2008 08:43 AM
Hi.,

I want to call some function when the user force to terminate the perl program.

For example.,
I write a program to create a file when the program starts...
Also I need to delete a file when the user force to terminate the process

i.e Calling some function when the user hitting Ctrl+C in perl command prompt..

Looking for Ur suggestions.

Thanks.
numberwhun's Avatar
numberwhun
Forum Leader
2,032 Posts
May 6th, 2008
03:10 AM
#2

Re: How to call on terminate function in perl
Quote:
Hi.,

I want to call some function when the user force to terminate the perl program.

For example.,
I write a program to create a file when the program starts...
Also I need to delete a file when the user force to terminate the process

i.e Calling some function when the user hitting Ctrl+C in perl command prompt..

Looking for Ur suggestions.

Thanks.


I think I know what you are asking to do, but I don't know if its possible or how to do it. Hopefully one our experts will have an answer for you.

Regards,

Jeff

Reply
KevinADC's Avatar
KevinADC
Expert
3,279 Posts
May 6th, 2008
07:08 AM
#3

Re: How to call on terminate function in perl
I'm not sure how wise this is but using a simple example:

Expand|Select|Wrap|Line Numbers
  1. local $SIG{INT} = sub { print "good-bye\n"; exit(0); };
  2. while (1){
  3. print "hello\n";
  4. sleep(2);
  5. }


I would read up on signals more and see what the perl documentation has to suggest. A place to start:

http://perldoc.perl.org/perlipc.html

Reply
cnsabar's Avatar
cnsabar
Member
36 Posts
May 7th, 2008
09:45 AM
#4

Re: How to call on terminate function in perl
Quote:
I'm not sure how wise this is but using a simple example:

Expand|Select|Wrap|Line Numbers
  1. local $SIG{INT} = sub { print "good-bye\n"; exit(0); };
  2. while (1){
  3. print "hello\n";
  4. sleep(2);
  5. }


I would read up on signals more and see what the perl documentation has to suggest. A place to start:

http://perldoc.perl.org/perlipc.html


Hi Kevin,

Thanks for your help and suggestion.,
Using the above logic,
Now I can able to produce the expected output in my program.

Thanks,

Reply
cnsabar's Avatar
cnsabar
Member
36 Posts
May 24th, 2008
01:33 PM
#5

Re: How to call on terminate function in perl
By Using this function , its works fine when the I press Ctrl + C
But it wont reflect anything when I close the command prompt using close icon on the right corner of the window.



Quote:
Hi Kevin,

Thanks for your help and suggestion.,
Using the above logic,
Now I can able to produce the expected output in my program.

Thanks,

Reply
KevinADC's Avatar
KevinADC
Expert
3,279 Posts
May 25th, 2008
07:04 PM
#6

Re: How to call on terminate function in perl
I don't know.

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,847 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Perl Forum Contributors