Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

oo code don't want to die !

by iza (Monk)
on Nov 28, 2001 at 22:38 UTC ( [id://128136]=perlquestion: print w/replies, xml ) Need Help??

iza has asked for the wisdom of the Perl Monks concerning the following question:

hi :) sorry if the question sounds stupid, but i've read the die() doc, perlman::perlvar, and the oo tutorial, and haven't found nothing
sooo, what i'd like to do is to catch an interruption signal, and stop the program (doesn't look so hard ;]) - the point is that, as the program creates & calls some objects, it seems i have to wait for these objects to return before killing them (seems logical so far)
what i don't understand, is that those objects are created & used in a foreach loop, and my code seems to kill only the CURRENT object - then the loop goes on and creates the next object, while i'd like it to stop
maybe it's as simple as "your SIG{INT} isn't in the right place" or maybe is that i totally misunderstood perl's behaviour (hopefully not) ...
here is the code (part of)
my @list = split (....) $SIG{INT} = \&catch_zap; foreach my $nam (@list) { ## Create foo object my $foo = new SUB::foo; $foo->name($nam); ... my ($result, $error_message)=$foo->run; ... ## Save Data my $file = ...; open FH , "> $file" || die "Couldn't open file: $!"; ... print FH "$result \n"; close FH; } sub catch_zap { my $signame = shift; &Warning("Somebody sent me a SIG$signame - trying to clean\n"); die "HEY I'M **done** STOP THIS PROGRAM __NOW__"; }

i let the file part in the foreach loop just to show there was something done after the call to "run"
so what happens is that the current object actually gets killed, i see the message from die, but the loop continues : the file is created, and then started again with a new object
if anyone knew where i fail, or how i could do to actually kill the program (exit the foreach loop, don't create the file, don't create nor call anymore object), i'd be really happy :))
thanx for having read ;]

Replies are listed 'Best First'.
Re: oo code don't want to die !
by dws (Chancellor) on Nov 29, 2001 at 03:41 UTC
    The section on signal handling in perlman:perlipc suggests that although die can be used to jump out of handler (to an enclosing eval), it's better to adopt the strategy of setting a variable from within the handler, then checking the variable outside of the handler. In your case, this would look like:
    my $caught_sigint = 0; local $SIG{INT} = sub { $caught_sigint = 1 }; foreach my $name ( @names ) { my $foo = new SUB::foo; $foo->name($name); ... last if $caught_sigint; } if ( $caught_sigint ) { die "<<pithy message here>>\n"; }
      that's great ! it now stops & exit the loop :)
      it crashes badly from times to times tho, because sometimes the ^C interrupts an object that is doing "in-interrupt-able" stuff (for instance, while an XML::Parser is connecting to retrieve the dtd - things like that)
      but those are small things to fix (hopefully ...), and i'm really happy with your help :) - thanx for "translating" perlman:perlipc to me, i (obviously !) haden't properly understood this part
Re: oo code don't want to die !
by maverick (Curate) on Nov 28, 2001 at 22:50 UTC
    hmmm...is it possible that there is a signal handler for INT inside SUB::foo? Try using USR1, INT has some pretty common uses, perhaps your catch_zap is being over written somewhere...

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

      no there is no signal handler in SUB::Foo - maybe i should write one in foo ? but it seemed more logical to me to catch it from the main program ...
      basically i wrote all classes (yes, foo calls other classes ...), and the only SIG{INT} is the one in the main code (the one i posted)
      where could i find some doc on USR1 ?
      anyway, thanks, many thanks for your answer :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://128136]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2025-02-09 01:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (95 votes). Check out past polls.