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

Hi i have written a script that would prompt for a username and password. And When i type by password during runtime it should display like "********" is there a way to do it. As i am using that in a aix server , i am not authorized to install any modules "::Crypt".

2006-07-21 Retitled by Arunbear, as per Monastery guidelines
Original title: 'password'

  • Comment on Prompt for password and display it as asterisks (******)

Replies are listed 'Best First'.
Re: Prompt for password and display it as asterisks (******)
by davorg (Chancellor) on Jul 21, 2006 at 07:35 UTC
      Hi THank you that was very helpful
Re: Prompt for password and display it as asterisks (******)
by McDarren (Abbot) on Jul 21, 2006 at 06:00 UTC
    Is this a CGI or a CLI script?

    If the former, then then you can just use a password field. From the CGI docs:

    CREATING A PASSWORD FIELD print $query->password_field(-name=>'secret', -value=>'starting value', -size=>50, -maxlength=>80); -or- print $query->password_field('secret','starting value',50,80 +); password_field() is identical to textfield(), except that its c +ontents will be starred out on the web page.

    If it's a CLI script (and you can't/wont use an external module), then you can just put the terminal into "no echo" mode with a system call to stty. The following example is from perldoc perlfunc:

    system "stty -echo"; print "Password: "; chomp($word = <STDIN>); print "\n"; system "stty echo";

    For a more portable solution, you'll need to use Term::ReadKey

    Cheers,
    Darren

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Prompt for password and display it as asterisks (******)
by herveus (Prior) on Jul 21, 2006 at 10:56 UTC
    Howdy!

    This is code I use.

    use IO::Prompt; my $pswd = prompt("password for <your name here>: ", -e => '*');
    The -e flag gives the character to echo.

    yours,
    Michael