Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Waiting before executing $SIG{INT} sub

Status
Not open for further replies.

perlnewbie9292

Programmer
Jul 15, 2009
25
US
Hello Perl experts,

I am pretty new to Perl and trying to figure out how to do the following.

I have a script which I've added a subroutine that intercepts $SIG{INT} (control -c) key combination.

Now is there anyway/how can I tell my subroutine to get to a certain point in the script (exit(0) using code below) or wait until the script has completed running before exiting?

What I want to avoid is when someone hits control-c the script finishes what it's doing before quitting, this way if it's in the middle of processing something is not half done.

Thanks for the help in advance.

Code:
$SIG{INT} = \&waitAndExit;

main();

sub main {
     'main program here';
     exit(0); #once it gets to this exit(0) then exit.
}
    

sub waitAndExit {
    'what to add here so that this waits until the script gets to the exit(0) above';
}
 
HI,

Once you hit cntrl-C ,the program controll is taken by waitAndExit ,and any other parts of "main" will not be executed anymore ,unless they were started in some kind of background/threading method.
Unless "main" contains some system commands that are ran in background - then you could consider checking their status/flags/resulted files inside the "waitAndExit" sub

Long live king Moshiach !
 
I disagree. As long as your signal handler doesn't exit the programme, control should return to where it was when the interrupt was handled. So you shouldn't need to do anything at all in waitAndExit really. You could of course just use $SIG{INT} = 'IGNORE' to ignore Ctrl-C completely.

Of course you can reassign your signal handler around the important bits, something like this:

Code:
[gray]#!/usr/bin/perl -w[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$wantToQuit[/blue] = [fuchsia]0[/fuchsia][red];[/red]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]waitAndExit[/maroon][red]([/red][red])[/red] [red]{[/red]
        [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]One moment, I just gotta finish this bit...[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
        [blue]$wantToQuit[/blue]=[fuchsia]1[/fuchsia][red];[/red]
[red]}[/red]

[olive][b]while[/b][/olive] [red]([/red]![blue]$wantToQuit[/blue][red])[/red] [red]{[/red]
        [blue]$SIG[/blue][red]{[/red][purple]INT[/purple][red]}[/red] = [red]'[/red][purple]waitAndExit[/purple][red]'[/red][red];[/red]
        [black][b]print[/b][/black] [red]"[/red][purple]doing important stuff[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
        [url=http://perldoc.perl.org/functions/sleep.html][black][b]sleep[/b][/black][/url][red]([/red][fuchsia]3[/fuchsia][red])[/red][red];[/red]
        [black][b]sleep[/b][/black][red]([/red][fuchsia]3[/fuchsia][red])[/red][red];[/red]
        [url=http://perldoc.perl.org/functions/exit.html][black][b]exit[/b][/black][/url] [olive][b]if[/b][/olive] [blue]$wantToQuit[/blue][red];[/red]
        [blue]$SIG[/blue][red]{[/red][purple]INT[/purple][red]}[/red] = [red]'[/red][purple]DEFAULT[/purple][red]'[/red][red];[/red]
        [black][b]print[/b][/black] [red]"[/red][purple]doing not-so-important stuff[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
        [black][b]sleep[/b][/black][red]([/red][fuchsia]3[/fuchsia][red])[/red][red];[/red]
[red]}[/red]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top