Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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

by Eyck (Priest)
on Jun 07, 2006 at 08:07 UTC ( #553981=perlquestion: print w/replies, xml ) Need Help??

Eyck has asked for the wisdom of the Perl Monks concerning the following question:

When it comes to GUI toolkits, it's easy, but what is the best way to ask about password (*****) on Win32 console?
Update Looks like I missed this one - there exists Term::ReadPassword::Win32 which seems compatible with Term::ReadPassword,
use Term::ReadPassword::Win32; $pass=read_password("prompt: ");
it does not support printing "*" though

Replies are listed 'Best First'.
Re: Best way to ask for password (cross-platform or Win32)
by zentara (Archbishop) on Jun 07, 2006 at 12:26 UTC
    Check out CLI must not echo password or here is a Term::ReadKey example.(you may need to adjust $ord for backspace if in an xterm, but it works in console, etc)
    #!/usr/bin/perl use strict; use Term::ReadKey; my $pwd = mask_pwd(); print "\n$pwd\n"; sub mask_pwd{ ReadMode(3); my $password = ''; print "Enter Password\n"; while(1){ my $ord = getord(); if ($ord == 10){last} # "Enter" if ($ord == 127) { # ie "Backspace" chop($password); print chr(8),' ',chr(8); next; } if($ord == 27){clearstream();$ord="\0"} if($ord != "\0"){ $password .= chr($ord); print '*'; } } ReadMode(0); return $password; } sub getord{ my $char; if (defined ($char = ReadKey(0))){} return ord($char); } sub clearstream{ while(1){ my $char; if (defined ($char = ReadKey(-1)) ){}else{return} } return ; }

    I'm not really a human, but I play one on earth. flash japh
Re: Best way to ask for password (cross-platform or Win32)
by john_oshea (Priest) on Jun 07, 2006 at 10:58 UTC

    IO::Prompt may be worth a look at - in particular the '-e' option.

Re: Best way to ask for password (cross-platform or Win32)
by kwaping (Priest) on Jun 07, 2006 at 15:00 UTC
    it does not support printing "*" though

    I checked the source, and it appears that it does. Try setting $Term::ReadPassword::USE_STARS = 1 and see what happens.

    It's all fine and dandy until someone has to look at the code.
      I'm on 0.05, and it only appeared in 0.06:
      0.06  Sun Aug 28 13:07:00 2005
      	- Setting $Term::ReadPassword::USE_STARS to a true value now
      	  echoes stars for password characters.
      and it looks like Term::ReadPassword::Win32 does not support that yet.

      IO::Prompt looks OK though, but it loads a boatload of dependencies on Win32, and it still haven't installed itself, so I don't even know if it's working.

Re: Best way to ask for password (cross-platform or Win32)
by youwin (Beadle) on Jun 23, 2009 at 00:31 UTC

    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 +/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";
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://553981]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2023-02-04 03:17 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (30 votes). Check out past polls.