#!/usr/bin/perl -w use strict; package GetPassword; use Carp; our @ISA = qw(Exporter); our $VERSION = 1.001; my $get_rid_of_warning = $VERSION; our @EXPORT = qw( Get_Password ); sub Get_Password { my $flush = $|; $| = 1; # flush output to screen byte by byte my $opt = shift || ""; $opt = substr $opt,0,1; my ($bs, $ws, $bullet) = ("\b", " ", $opt); ($bs, $ws, $bullet) = ("", "", "") unless $opt; my $pass = ""; while (1) { my $ch = getone(); # Return character, end of getting password $ch =~ /[\r\n]/ && do { print "\n"; $| = $flush; # return to previous setting return $pass; }; # Backspace/delete key entered $ch =~ /[\b\x7F]/ && do { if (length ($pass) > 0) { chop $pass; print "$bs$ws$bs"; } next; }; # Warn for non-ascii characters if (ord($ch) < 32) { print "\7"; next; }; # Valid password character enetered $pass .= $ch; print $bullet; } } # ============================================================================ # Terminal I/O program to get un-echoed unbuffered input # # Take from the Camel Book (3rd edition) # (Programming Perl, Larry Wall, O'Reilly), p.906 # ============================================================================ BEGIN { use POSIX ":termios_h"; my ($term, $oterm, $echo, $noecho, $fd_stdin); $fd_stdin = fileno(STDIN); $term = POSIX::Termios->new(); $term->getattr($fd_stdin); $oterm = $term->getlflag(); $echo = ECHO | ECHOK | ICANON; $noecho = $oterm & ~$echo; sub cbreak { $term->setlflag($noecho); $term->setcc(VTIME, 1); $term->setattr($fd_stdin, TCSANOW); } sub cooked { $term->setlflag($oterm); $term->setcc(VTIME,0); $term->setattr($fd_stdin, TCSANOW); } sub getone { my $key = ""; cbreak(); sysread(STDIN, $key, 1); cooked(); return $key; } } END { cooked() } 1