Perl password masking in Linux

Most solutions for this seem to point you to Term::ReadKey, which looks something like this:
ReadMode( noecho => STDIN );
my $pass = <STDIN>;
ReadMode( restore => STDIN );

However, I did not have that module on my servers, and the Linux admins get fussy about installing new ones. Fortunately, it does not seem that great of a loss, as system calls via Perl are not really more complicated – the code to hide the password looks nearly identical, and character substitution would still require a loop. It looks like this is geared towards making scripts more portable.

Anyway, here is some code.

Method 1 : Turns off echo so you don’t see anything as it is typed.
system("stty -echo");
$pass=<STDIN>;
chomp($pass);
system("stty echo");

Method 2 : Turns off echo and buffering. This lets you read one character at a time and write an asterisk instead of echoing the character to the terminal. To get the full password, you just concatenate the characters as they are read in. In this case, you check for the end of input using ord() – you are looking for something like CR or LF, but really anything below ascii 32 should not be in a password.
system ("stty -echo -icanon");
while (sysread STDIN, $a, 1) {
if (ord($a) < 32) { last; } $pass .= $a; syswrite STDOUT, "*", 1; } system ("stty echo icanon");

http://lists.freebsd.org/pipermail/freebsd-questions/2005-August/095047.html
http://www.perlmonks.org/?node_id=163606