http://www.perlmonks.org?node_id=590211

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

Would there be a way to edit STDIN _BEFORE_ it is displayed on the screen?

SMayper and I are going to design, for fun, a file system with user logins, but we don't want the password to actually show on the screen when they type it to log in.
One of our competition questions requests that we display input using asterisks, so we know that it can't be something too difficult and obscure if we're asked to do it in competition (where most of the questions have us counting certain character -- thank you, perl. If only they knew...).

Thanks in advanced!
C(qw/74 97 104 112/);sub C{while(@_){$c**=$C;print (map{chr($C!=$c?shift:pop)}$_),$C+=@_%2!=1?1:0}}

Replies are listed 'Best First'.
Re: Editing STDIN
by Corion (Patriarch) on Dec 16, 2006 at 15:54 UTC
Re: Editing STDIN
by vladdrak (Monk) on Dec 16, 2006 at 20:28 UTC
    You could use Term::InKey, like so:
    use Term::InKey; print "password: "; my $password = ReadPassword("*"); print "\nYour password is '$password'\n";
      Term::InKey is a good solution to the OP question, especially if he really does want to be able to modify characters before echoing, and he's just giving passwords as an example of this. One common use is uppercasing a sign-on or other field to indicate that it's case-insensitive.

      When doing this, remember to unbuffer STDOUT, as in:

      use warnings; use strict; use Term::InKey; use IO::Handle; STDOUT->autoflush(1); my $x=''; print "receiving chars\n"; while ($x ne 'x') { $x = ReadKey(); print uc($x); }
      Hope this helps.

      I'm sorry, but I downvoted this node, for two reasons:

      1) It adds nothing to Corion's reply, posted more than four and a half hours earlier.

      2) Like the earlier response, it still doesn't address one of the OP's main problems, namely: 'One of our competition questions requests that we display input using asterisks'...

      Edit: Sincere apologies. Ignore the above! I misread use Term::InKey; as use Term::ReadKey;

      vladdrak+++, Not_a_Number--

Re: Editing STDIN
by jdporter (Paladin) on Dec 16, 2006 at 17:44 UTC

    A classic example of an XY Problem. Your real need is to obscure a password being entered, but you ask a different question, thinking that's the only or best way to approach it.

    That being said, I think the question you asked is an intriguing one.

    We're building the house of the future together.