Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Reading a password with Term::ReadLine

by Starky (Chaplain)
on May 11, 2004 at 03:33 UTC ( #352298=perlquestion: print w/ replies, xml ) Need Help??
Starky has asked for the wisdom of the Perl Monks concerning the following question:

I would like to get a username / password combination with Term::ReadLine in a way that echos the username as it is typed but obscures the password.

With

my $term = new Term::ReadLine; print $term->ReadLine;
I can tell that my Term::ReadLine is using Term::ReadLine::Perl, which is, ahem, lacking in documentation. I can see from this example that
$attribs = $term->Attribs; $attribs->{redisplay_function} = $attribs->{shadow_redisplay}; $password = $term->readline("Password: "); $term->remove_history($term->where_history);
would do the trick if I was using a Term::ReadLine::* that supported the shadow_redisplay attribute. But alas, Term::ReadLine::Perl does not:
starky@freak bin $ ./readline-password-test.pl Term::ReadLine uses [Term::ReadLine::Perl] Username: uname Password: passwd The username is [uname] and the password is [passwd] starky@freak bin $

Suggestions?

Comment on Reading a password with Term::ReadLine
Select or Download Code
Re: Reading a password with Term::ReadLine
by asarih (Hermit) on May 11, 2004 at 03:43 UTC
    Do you have to use Term::ReadLine? If not, see perldoc -q password.
                 First, you put the terminal into "no echo" mode,
                 then just read the password normally.  You may do
                 this with an old-style ioctl() function, POSIX
                 terminal control (see POSIX or its documentation the
                 Camel Book), or a call to the stty program, with
                 varying degrees of portability.
    
                 You can also do this for most systems using the
                 Term::ReadKey module from CPAN, which is easier to
                 use and in theory more portable.
    
                     use Term::ReadKey;
    
                     ReadMode('noecho');
                     $password = ReadLine(0);
    

    Probably easier using Term::ReadKey.

      You missunderstand. He wants to read a password and echo non-descriptive characters for every letter you type, for example, asterisks.
Re: Reading a password with Term::ReadLine
by TilRMan (Friar) on May 11, 2004 at 07:12 UTC
    use Term::ReadKey; ReadMode('noecho'); ReadMode('raw'); my $pass = ''; while (1) { my $c; 1 until defined($c = ReadKey(-1)); last if $c eq "\n"; print "*"; $pass .= $c; } ReadMode('restore'); print "\n[$pass]\n";

    Thanks to asarih and BUU.

      ++ Great snippet, I actually have a use for this.
      May I suggest inserting the following, just before your 'use Term::ReadKey;' line?
      $|=1; #Turn on AutoFlush

      On my system (Sun solaris 9 perl 5.8.0) I was consistently seeing 1 less asterisk until I hit the enter key. This should provide a little less confusion for folks entering passwords and not seeing asterisks echoed immediately.
      Thanks.


      Very funny Scotty... Now PLEASE beam down my PANTS!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2014-10-02 06:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (49 votes), past polls