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

[Fun With Perl] Re: (MacPerl) Invisible password



	# 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 :-)

	# Note that the <STDIN> solution would work on Unix perl, modified
	# slightly as follows:

	#!/usr/local/bin/perl -w
	# Warning - Unix specific solution!

	print "Enter your password: ";
	`stty -echo`;
	chomp($pass = <STDIN>);
	print "\nYour password is $pass\n";
	`stty echo`;
	__END__

	#(remember to add the extra \n; the one the user types isn't echoed!)

	# The difference is that raw mode is _really_ raw. echo / -echo turn
	# on/off echoing of characters to the screen but don't otherwise fool
	# with the processing of those characters.
-- --
        |\      _,,,---,,_       Vicki Brown <vlb@cfcl.com>
  ZZZzz /,`.-'`'    -.  ;-;;,_   Journeyman Sourceror: Scripts & Philtres
       |,4-  ) )-,_. ,\ (  `'-'  P.O. Box 1269  San Bruno  CA  94066
      '---''(_/--'  `-'\_) http://www.cfcl.com/~vlb  http://www.macperl.org

==== Want to unsubscribe from this list? (Don't you love us anymore?)
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org

</x-flowed>