in reply to Using traps to autoincrement / decrement variables
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:
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.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--; } }
Cheers,
Paul
|
|---|