in reply to Using traps to autoincrement / decrement variables

Fletch's answer above already covers signals quite well, so I won't mention them here. Instead I'll present a small alternative.

If your program is always going to be attached to a terminal of some sort (ie, it won't run in the background or as a daemon), you can use Term::ReadKey to check for individual keystrokes. For example:

use Term::ReadKey; ReadMode 3; # cbreak mode while ($stuff) { # Ping hosts and whatnot. if (defined($key = ReadKey(-1))) { # nonblocking read $key eq 'q' && exit 0 or $key eq '+' && $foo++ or $key eq '-' && $foo--; } }
The above code keys checks for eachstrokes and either increments or decrements $foo, or exits the program if 'q' is pressed. This is a much more user-friendly and useful way of running should you have someone seated at the keyboard.

Cheers,
Paul