#!perl
use Config;
use File::Basename qw(&basename &dirname);
use Cwd;

$origdir = cwd;
chdir dirname($0);
$file = basename($0, '.PL');
$file .= $^O eq 'VMS' ? '.com' : '.pl';

open OUT,">$file" or die "Can't create $file: $!";

print "Extracting $file (with variable substitutions)\n";

print OUT <<"!GROK!THIS!";
$Config{startperl} -w
!GROK!THIS!

# In the following, perl variables are not expanded during extraction.

print OUT <<'!NO!SUBS!';
# file: interrupt.pl
# Figure 2.4: Catching the sigINT signal

use strict;
my $interruptions = 0;
$SIG{INT} = \&handle_interruptions;

while ($interruptions < 3) {
  print "I'm sleeping.\n";
  sleep(5);
}

sub handle_interruptions {
  $interruptions++;
  warn "Don't interrupt me!  You've already interrupted me ${interruptions}x.\n";
}

!NO!SUBS!
close OUT or die "Can't close $file: $!";
chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';
chdir $origdir;
