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

Hi,
Could anyone help me to read a password from the user?
The user, while entering, should not be able to see the password...or atleast see *s
I tried term::readpassword and term::readkey, but both seem tricky... i've got perl 5.8.
Thanks...

Replies are listed 'Best First'.
Re: Reading a password from the user
by Aragorn (Curate) on Apr 04, 2003 at 11:53 UTC
    Using Term::ReadKey isn't that trickey:
    #!/usr/bin/perl -w use Term::ReadKey; ReadMode 'noecho'; my $password = ReadLine 0; chomp($password); ReadMode 'normal'; # and use $password here...

    Arjen

Re: Reading a password from the user
by Joost (Canon) on Apr 04, 2003 at 11:59 UTC
    And Term::ReadPassword seems even easier:

    From the docs

    use Term::ReadPassword; my $password = read_password('password: ');
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
      Though, I've tried this before, I downloaded both the modules (Term::ReadKey, Term::ReadPassword) from CPAN...
      They dont seem to work when i run them. I'm able to 'use' it but the modules don work. I have active perl 5.8 for windows.
      ... but it seems that once you have it properly installed, its easy after that...
      Any Alternatives?...thanks!
        Downloading them is not. You have to install them.

        How do you do that?

        Well, if you prefer to do it by hand as you evidently do, you should read the README file that is part of the tarball (.tar.gz file) you downloaded from CPAN.

        First you unpack it with gunzip and tar, then:

        perl Makefile.PL make make test make install

        For the final, make install step, you need to be root.

        --
        Regards,
        Helgi Briem
        helgi AT decode DOT is

        For Active State's perl on Windows, there is a bundled module installer. I would recommend trying to use that to get the modules properly set up to use in AS perl.

Re: Reading a password from the user
by JamesNC (Chaplain) on Apr 04, 2003 at 15:08 UTC
    Check out my previous post Node 224221 The code there shows you how to grab a password from the command line (Win32) and echo back a *, and it handles rubbing out the * when you hit backspace too.

    Cheers,
    JamesNC
Re: Reading a password from the user
by Abigail-II (Bishop) on Apr 04, 2003 at 11:37 UTC
    perldoc -f crypt

    Abigail

      Note that the author's original question had nothing to do about encrypting strings :P


      My bad, Abigail-II's reply below shows how I was a little too quick to post. It seemed odd to me that Abigail-II would post irrelevant information, as I know how well she knows her stuff :)


      If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

        So? Did you actually bother to look up what perldoc -f crypt displays? Or did it give you more satisfaction to write a stupid reply than to do some trivial research. Let me quote it for you:
        Here's an example that makes sure that whoever runs this program knows their own password: $pwd = (getpwuid($<))[1]; system "stty -echo"; print "Password: "; chomp($word = <STDIN>); print "\n"; system "stty echo"; if (crypt($word, $pwd) ne $pwd) { die "Sorry...\n"; } else { print "ok\n"; }

        Abigail

Re: Reading a password from the user
by dbush (Deacon) on Apr 05, 2003 at 10:32 UTC

    I wrote this ages ago for a few quick scripts. It's less than slick, for example it uses global variables, but I found it useful. The Win32::Console module this script uses should be installed with AS.

    Regards,
    Dom.

Re: Reading a password from the user
by katgirl (Hermit) on Apr 04, 2003 at 13:56 UTC
    The user, while entering, should not be able to see the password...or atleast see *s
    <input type="password" name="password" size="20">
    Make a form, using the above, to send the password to your script. The user will see ****** when they type, but the script will recieve the password in the same way as any other text. If you have the password encrypted in a file, you then crypt the password sent to the script, and then compare the two crypted passwords. I don't see why you need special modules to do this... could you explain some more?

      The explanation is that this case is getting the password from a command line and not via a web form.