Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Preventing ^C stopping / killing a program - Perl

Here's a demonstration - in Perl - that shows you how to avoid a ^C (Control C) dropping you straight out of a program.

Have you ever accidentally hit ^C in the wrong window and terminated a long-running process just before it finished ... well, by setting $SIG{INT} to the address of a sub you want to run, you can divert the signal in Perl. The example code here simulates a long running process with a loop of 60 short sleeps (naps?) ...

$SIG{"INT"} = \&nowayjose;
 
$| = 1;
 
for (1..60){
  print "dot";
  sleep 1;
 
  if ($rq) {
    exit if (time() - $recent < 4) ;
    print "\nNah\n";
    $rq = 0;
    $recent = time();
    }
  }
 
sub nowayjose {
  $rq = 1;
  }


You'll note that all my extra "sub" does is set a flag so that the interrupt can be nicely handled at the end of the loop, that the handler turns the interrupt flag back off, and that I've written the program so that a second ^C within 4 seconds WILL cause it to exit.

As an afterthought ... if you disable ^C completely (i.e. if you don't use the 4 second trick), how will you get out of the program? Well ... you'll still be able to suspend it with ^Z then kill it with kill %1, or you'll be able to find its process id and use a kill -9
(written 2008-12-05, updated 2008-12-07)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P667 - Perl - Handling Huge Data
  [3375] How to interact with a Perl program while it is processing data - (2011-07-31)
  [3374] Speeding up your Perl code - (2011-07-30)
  [2834] Teaching examples in Perl - third and final part - (2010-06-27)
  [2806] Macho matching - do not do it! - (2010-06-13)
  [2805] How are you getting on? - (2010-06-13)
  [2376] Long job - progress bar techniques (Perl) - (2009-08-26)
  [1920] Progress Bar Techniques - Perl - (2008-12-03)
  [1397] Perl - progress bar, supressing ^C and coping with huge data flows - (2007-10-20)
  [975] Answering ALL the delegate's Perl questions - (2006-12-09)
  [762] Huge data files - what happened earlier? - (2006-06-15)
  [639] Progress bars and other dynamic reports - (2006-03-09)


Back to
Making it all worthwhile
Previous and next
or
Horse's mouth home
Forward to
Introduction to Object Oriented Programming
Some other Articles
Melksham Oak Community School, Melksham, Wiltshire
Team changes at Well House - looking forward
Flash (client) to PHP (server) - example
Introduction to Object Oriented Programming
Preventing ^C stopping / killing a program - Perl
Making it all worthwhile
Flurinci knows Raby Lae PHP and Jeve
Romeo and Julie
Transition
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1924_Pre ... -Perl.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat