in reply to Re^3: how to check for keyboard input
in thread how to check for keyboard input
s/&/\\&/g; Otherwise, safe_exit is immediately called. Also, I tend to wrap sub's similar to safe_exit in lexical blocks and prevent them from being called more than once. e.g.
use Term::ReadKey; { my $called = 0; sub safe_exit { print "safe_exit called $called times before\n"; return if $called++; # disconnect from DB, etc. (things that should only be done on +ce) ReadMode 'normal'; print "Exiting safely\n"; print "exit(@_)\n" if @_; exit @_; } } $SIG{$_} = \&safe_exit for qw/INT QUIT __DIE__/; END { safe_exit(); } ReadMode 'cbreak'; print "Entering loop. Try: kill -INT $$, to see that I exit safely.\nH +it 'x' to see me die.\n"; while (1) { next unless defined(my $key = ReadKey); print "In the loop, got: $key\n"; last if $key eq 'q'; die "with this error message" if $key eq 'x'; }
|
|---|