in reply to Best way to ask for password (cross-platform or Win32)
This function worked well for me at work (windows), and at home (linux)
it uses the same format as IO::Prompt (which didnt work on windows last time i checked), but only does one of its features (the -echo option). So If I ever needed I could replace the use statement and still have a working prompt hopefully
Then to use it:use Term::ReadKey; sub prompt { my ($prompt, %args) = @_; local $| = 1; my $phrase = ''; print $prompt; ReadMode 'cbreak'; while (1) { my $c = ReadKey ~0/2-1; #windows workaround http://rt.cpan.org +/Public/Bug/Display.html?id=27944 if ($c =~ /[\r\n]/) { print "\n"; last; } elsif ($c eq "\b" || ord $c == 127) { next unless length $phrase; chop $phrase; next if !defined $args{-echo}; print map $_ x length $args{-echo}, "\b", " ", "\b"; } elsif (ord $c) { $phrase .= $c; print $args{-echo} if defined $args{-echo}; } } ReadMode 'restore'; $phrase; }
my $pwd = prompt 'enter password: ', -echo => '*' print "$pwd\n";
|
---|
In Section
Seekers of Perl Wisdom