473,608 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perl's DESTROY and Signal Handling

11 New Member
I'm pretty new at this, and I'm trying to figure out how Perl's classes work with signals.
Specifically, it doesn't seem that a class's DESTROY function is called when you Ctrl-C the program.

I tried using
Expand|Select|Wrap|Line Numbers
  1. use sigtrap qw(handler DESTROY INT QUIT);
, but I'm not even sure this is the proper way to catch the signal. Either way, it seems I no longer receive a reference to my object when DESTROY is called. I keep getting this error:

Expand|Select|Wrap|Line Numbers
  1. Can't locate object method "time_passed" via package "INT" (perhaps you forgot to load "INT"?) at /var/local/bush/lib/ASH/Basic.pm line 113.
  2.  
Here is my DESTROY function:
Expand|Select|Wrap|Line Numbers
  1. sub DESTROY {
  2.     my $self = shift;
  3.  
  4.     my $runtime = time_passed(); 
  5.     print "Total run time: $runtime\n";
  6.  
  7.     close(ERR);
  8.     close(WARN);
  9.  
  10.     -d $self->{DIR}{THRESHOLD} ? print "All files saved to $self->{DIR}{THRESHOLD}.\n" : print "Self destroyed.\n";
  11.     die("done.\n\n");
  12. }
  13.  
Thanks for the help!
Feb 19 '08 #1
6 4020
KevinADC
4,059 Recognized Expert Specialist
I'm pretty new at this, and I'm trying to figure out how Perl's classes work with signals.
Specifically, it doesn't seem that a class's DESTROY function is called when you Ctrl-C the program.

I tried using
Expand|Select|Wrap|Line Numbers
  1. use sigtrap qw(handler DESTROY INT QUIT);
, but I'm not even sure this is the proper way to catch the signal. Either way, it seems I no longer receive a reference to my object when DESTROY is called. I keep getting this error:

Expand|Select|Wrap|Line Numbers
  1. Can't locate object method "time_passed" via package "INT" (perhaps you forgot to load "INT"?) at /var/local/bush/lib/ASH/Basic.pm line 113.
  2.  
Here is my DESTROY function:
Expand|Select|Wrap|Line Numbers
  1. sub DESTROY {
  2.     my $self = shift;
  3.  
  4.     my $runtime = time_passed(); 
  5.     print "Total run time: $runtime\n";
  6.  
  7.     close(ERR);
  8.     close(WARN);
  9.  
  10.     -d $self->{DIR}{THRESHOLD} ? print "All files saved to $self->{DIR}{THRESHOLD}.\n" : print "Self destroyed.\n";
  11.     die("done.\n\n");
  12. }
  13.  
Thanks for the help!
Might be a question for perlmonks.com unless someone here understands whats happening by the description you have posted. Maybe you can't use DESTROY as a handler.
Feb 19 '08 #2
muppetjones
11 New Member
So I figured out a round about solution. I eventually was able to get DESTROY called as the signal handler; however, this does not kill the program unless you have a
Expand|Select|Wrap|Line Numbers
  1. die()
or some other kill function inside the DESTROY function.

Obviously, this presents a small problem if you're through with your object before you're through with your script.

Here's somewhat of a solution:
Expand|Select|Wrap|Line Numbers
  1. use sigtrap qw(handler the_rest_is_silence INT QUIT);
  2. sub the_rest_is_silence { die(); } # Calling this on a kill signal ensures DESTROY is called
For some reason when this is in the .pm file, this works to help ensure DESTROY is called when you Ctrl+C. I don't really know why this is any different than not having it in there (since all you are doing is explicitly calling die() ), but it seems to work for me. Perhaps it is something to do with the Ctl+C signal vs. that sent from die().

Either way, if a class's DESTROY is not being called when the program is killed, this should help.
Mar 4 '08 #3
See Furst
1 New Member
Perl uses the %SIG hash to handle signals.. Not sure if this is wise.. DESTROY is for GC mostly.

however you can do
$SIG{TERM} = sub { $class->DESTROY() };

but again there is probably a better way to do it
Jul 20 '10 #4
Oralloy
988 Recognized Expert Contributor
I'm not going to reseach chapter and verse of the C-language and Posix specifications for signal handling, but they're good places to start your understanding.

Basically the Perl signal handling is the interpretive wrapping of the C-language signal handling mechanism.

EDIT:gahhhh - this may be wrong - you may have to retrieve the old handler from the %SIG hash yourself. My discussion is topical, I've done plenty of signal handling in C and C++, but nothing in Perl.

First off, when you register a new signal handler, Perl will hand you a link to the old one. This way you can save the link and chain to the old signal handler, so that behavioral integrity is maintained when a signal is recieved.

In your case, your handler can do all of its clean up and hand control to the handler that it replaced. That way, any modules besides yours, which are ctrl-C sensitive, can do their clean-ups, too.

Of course, the devil is in the details. If there will be multiple objects to DESTROY, you'll have to keep a list of them and make sure that they're all dealt with appropriately. Also, the list will have to be self-maintaining with weak references, so if an object is released it will be DESTROYED appropriately by the system and removed from the list....

Implementation gets ugly and I'm not entirely sure of the necessary implementation details. If you simply keep a list of objects, then DESTROY will never be called, because they will always be referenced. Thus the requirement for a list of weak references.

What it really comes down to is the level of robustness and reliability you require. Only you can answer that.

Good luck.
Jul 21 '10 #5
Jyoti Ballabh
115 New Member
from what I perceive is that you have to embed a kill function within a destroy signal
Jul 24 '10 #6
iohos
45 New Member
@Jyoti Ballabh
do u have embedding function? if yea, care to share?
Jul 25 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3449
by: David F. Skoll | last post by:
Hi, I'm tearing my hair out on this one. I'm trying to embed a Perl interpreter into a C program. I need to be able to create and destroy the interpreter periodically, but will never actually have two interpreters at the same time. On Red Hat Linux 7.3 with Perl 5.6.1, the attached program segfaults. On Red Hat 9 with Perl 5.8.0, it works perfectly. Save the program code as "test-embed-perl.c" and the build script as "buildte". ...
0
4193
by: John C. Worsley | last post by:
I've got an extremely inscrutable problem here using Perl's Term::ReadLine::Gnu module. I'm using Perl 5.8.0, readline 4.3 and Term::ReadLine::Gnu 1.14. The problem is specific to catching INT signals while in the readline() function. In C, when I've set a SIGINT handler using signal(), it is immediately called (even in readline()) when I hit CTRL-C. Likewise in Perl, if I am reading in a while() loop from STDIN, and I have set the...
2
3924
by: Lionel van den Berg | last post by:
Hi all, I'm trying to do some signal handling using the csignal library but I can't find and specific examples for C++. The signal function as defined in C takes a parameter that is the signal we are registering for, and a second parameter that is the function to call. What I want to do is specify the function to call as a function of an instance of "this" class e.g.
3
3512
by: Martin McCormick | last post by:
A C program contains several signal statements to remove a lock file if the program gets killed: /*Set up interrupt handler to catch ctrl-C so that lock file can be removed.*/ signal(SIGINT,crash); signal(SIGBUS,crash); signal(SIGSEGV,crash); This part works. Those signals cause the "crash.c" module to run and get rid of the lock. Is there a standard way to also print
4
2003
by: Eric Boutin | last post by:
Hi ! currently reading C docs, I think I'm reading docs about the stdc lib, but I'm not shure.. it talks about signals, does *all* OS implement the signal function, and use it well ? I mean.. I know Unixes do it, but what about windows ? does it implements all SIG* signal promptly ? Thanks ! -Eric Boutin
0
9737
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile. I need it to be compiled with threads. Anyone have any wisdom on how best to do this? Here's a transcript of my latest attempt. It's long; you might want to skip to the bottom, where I try "make" and the fatal errors start happening.
7
2206
by: Stanley S | last post by:
Hi, Are Signal Handling part of ANSI C? I am not able to find any reference of Sig Handling in Stephen Prata's "C Primer Plus". The usage of signals is to trap errors I guess. (It looks similiar to the concept of try-catch to me.) It seems to relate more to nix OS. Are signals handling part of Windows too?
2
1478
by: hg | last post by:
Hi, I posted an equivalent question earlier ... but am still not sure: I currently (under Linux) have a program that uses Queue.put (raw_input('')) in a signal handler and Queue.get() in the main/only thread. It works but I want to know if it is legal / what type of synchro API I have the right to use in a signal handler.
1
4812
by: 32Alpha | last post by:
Hi, first post here. First off, this IS a homework assignment for an operating systems class, but the question isn't "how do i do the assignment" but "why is my particular implementation not working". I've searched the forums here, but could not find anything particular to my problem, Googling and the linux manual pages haven't been much help either. What the program is supposed to do is create a very simple shell (that sits atop the...
0
8011
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8503
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8488
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8160
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8358
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
3972
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4036
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2479
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.