To ensure that the terminal is reset when your program exits, put the reset code into an END
block. For example, you could replace your _end_win
sub with:
END {
print "\n\r";
Term::ReadKey::ReadMode 'restore';
}
(I removed the lines resetting $|
and the select
ed output filehandle since the process is exiting anyhow, so they're about to become irrelevant.)
An END
block will always run when the program terminates in a "normal" way, such as calling exit
or die
or hitting the end of the executable code. It does not fire when the process terminates due to receiving a signal; it looks like you're handling the ctrl-C character directly, but you may want to consider adding a %SIG{INT}
handler as well, in case someone sends you a kill -2
.