Reputation: 5139
How do I prevent my Perl script from echoing what is typed into the terminal while it's running?
I tried messing around with system("stty -echo");
and then running system("stty echo");
at the end, but it still displays what I type once the script ends. I've been playing around with this test script.
#!/usr/bin/perl
use strict;
use warnings;
system("stty -echo");
for (1..15) {
print "$_\n";
sleep(1);
}
system("stty echo");
This could mess up the terminal if the second system called is never reached (maybe use an END
block, but that's not guaranteed to work either). This would also be Unix only and my script is run on Windows too.
I also found the module Term::Readkey
, but I would prefer not bringing in any other modules.
I also tried just closing STDIN
but that didn't work either.
What makes this problem easier is that I don't need to read STDIN
at all during execution, I can just ignore it.
I don't have much experience with this type of problem, hopefully it's easier than I'm making it out to be. Thanks!
edit: I'm on Perl 5.10 if that makes a difference.
Upvotes: 2
Views: 2920
Reputation: 107040
Take a look at Term::ReadKey:
use strict;
use warnings;
use Term::ReadKey;
ReadMode ( 'noecho' );
my $password = <STDIN>;
ReadMode ( 'normal' ); #Back to your regularly scheduled program
Unfortunately, it's not a standard module. And, I've had some problems with this on Windows.
Upvotes: 2