title: Breaking out of a while loop with system commands in Perl using Ctrl-C (SIGINT)?
body: <p>Consider the following example, <code>test.pl</code>:</p>
  
  <pre><code>#!/usr/bin/env perl
  
  use 5.10.1;
  use warnings;
  use strict;
  
  $SIG{'INT'} = sub {print "Caught Ctrl-C - Exit!\n"; exit 1;};
  
  $| = 1; # turn off output line buffering
  
  use Getopt::Long;
  my $doSystemLoop = 0;
  
  GetOptions( "dosysloop"=&gt;\$doSystemLoop );
  print("$0: doSystemLoop is:$doSystemLoop (use " . (($doSystemLoop)?"system":"Perl") . " loop); starting...\n");
  
  my $i=0;
  
  if (not($doSystemLoop)) { # do Perl loop
  
      while ($i &lt; 1e6) {
        print("\tTest value is $i");
        $i++;
        sleep 1;
        print(" ... ");
        sleep 1;
        print(" ... \n");
      }
  
  } else {                  # do system call loop
  
      while ($i &lt; 1e6) {
        system("echo","-ne","\tTest value is $i");
        $i++;
        system("sleep 1");
        system("echo","-ne"," ... ");
        system("sleep 1");
        system("echo","-e"," ... ");
      }
  
  }
  </code></pre>
  
  <p>So, if I call this program, so it uses a usual Perl loop, everything is as expected:</p>
  
  <pre><code>$ perl test.pl
  test.pl: doSystemLoop is:0 (use Perl loop); starting...
      Test value is 0 ...  ... 
      Test value is 1 ...  ... 
      Test value is 2 ... ^CCaught Ctrl-C - Exit!
  $
  </code></pre>
  
  <p>... that is, I hit Ctrl-C, program exits instantly. </p>
  
  <p>However, if the <code>while</code> loop's commands consist mostly of <code>system</code> calls, then it becomes nearly impossible to exit with Ctrl-C:</p>
  
  <pre><code>$ perl test.pl --dosysloop
  test.pl: doSystemLoop is:1 (use system loop); starting...
      Test value is 0 ...  ... 
      Test value is 1 ...  ... 
      Test value is 2 ... ^C ... 
      Test value is 3 ... ^C ... 
      Test value is 4 ... ^C ... 
      Test value is 5^C ... ^C ... 
      Test value is 6^C ... ^C ... 
      Test value is 7^C ... ^C ... 
      Test value is 8^C ... ^C ... 
      Test value is 9^C ... ^C ... 
      Test value is 10 ... ^C ... 
      Test value is 11^C ... ^C ... 
      Test value is 12^C ...  ... 
      Test value is 13^Z
  [1]+  Stopped                 perl test.pl --dosysloop
  $ killall perl
  $ fg
  perl test.pl --dosysloop
  Terminated
  $ 
  </code></pre>
  
  <p>So in the snippet above, I'm hitting Ctrl-C (the <code>^C</code>) like mad, and the program ignores me completely <code>:/</code> Then I cheat by hitting Ctrl-Z (the <code>^Z</code>), which stops the process and sets in the background; then in the resulting shell I do <code>killall perl</code>, and after that I execute the <code>fg</code> command, which places the Perl job back in the foreground - where it finally terminates due to the <code>killall</code>. </p>
  
  <p>What I would like to have, is run a system loop like this, with the possibility to break out of it/exit it with the usual Ctrl-C. Is this possible to do, and how do I do that?</p>