[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Password Dialog



At 16:51 -0400 98/08/05, Chris Nandor wrote:
> At 17.34 -0400 1998.08.05, w e b s l a v e wrote:
> >Is there an easy way to have a dialog not show the characters entered?

>
> >Otherwise, is there a STDIN solution?
>
> Yes.  This should work, I think:
>
> #!perl -w
> print "Enter your password: ";
> `stty raw`;
> chomp($pass = <STDIN>);
> print "Your password is $pass\n";
> `stty sane`;
> __END__
>
> But in actuality, it does not work for me.  Hrm.  I get:

The problem with this is that, once in raw mode, "no key pressed yet" is
interpreted as EOF. (It's in the book :-).  Which means that $pass is set
to nothing (instantly) and -w complains about an uninitialized value (or
$pass).

It doesn't work without -w either, but it's less noisy:

   Enter your password: Your password is

Anyhow, you need to get tricky to catch the characters one at a time, and
process them appropriately.  Presuming you want the user to be able to
press return and have thet enter the password, try:

#!perl -w
print "Enter your password: ";
`stty raw`;
while (1) {
   $char = getc() while (!defined($char));
   if ($char =~ /\n/) {
        print $char;
        last;
   }
   $pass .= $char;
   undef $char;
}
`stty sane`;
print "Your password is $pass\n";
__END__

When run, this produces:
   Enter your password:

   Your password is foobonnet

(yeah, that's what I typed :-)

---
Vicki Brown, vlb@cfcl.com        |\      _,,,---,,_
Journeyman Sourceror      ZZZzz /,`.-'`'    -.  ;-;;,_
Scripts & Philtres             |,4-  ) )-,_. ,\ (  `'-'
http://www.cfcl.com/~vlb      '---''(_/--'  `-'\_)
P.O. Box 1269 San Bruno, CA  94066

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch