Using %SIG to define our own signal handlers
#!/usr/bin/perl use warnings; use strict; my $count = 3; $| = 1; $SIG{ 'INT' } = \&stop; while ( $count ) { print( "Hi!" ); print( "\n" ); sleep( 1 ); } sub stop { $SIG{ 'INT' } = \&stop; $count--; print( "$count\n" ); unless ( $count ) { print( "line...\n" ); } }