Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Best way to ask for password (cross-platform or Win32)

by youwin (Beadle)
on Jun 23, 2009 at 00:31 UTC ( #773803=note: print w/replies, xml ) Need Help??


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

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; }
Then to use it:
my $pwd = prompt 'enter password: ', -echo => '*' print "$pwd\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://773803]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2020-10-25 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (249 votes). Check out past polls.

    Notices?