[Perl][终端应用]输入密码,使其不显示在屏幕上

There's more than one way to do it!
https://metacpan.org http://perlmonks.org
回复
头像
PerlMonk
渐入佳境
渐入佳境
帖子: 49
注册时间: 2016年09月19日 10:20
联系:

[Perl][终端应用]输入密码,使其不显示在屏幕上

帖子 PerlMonk »

支持退格
=info Author : PAKTC/PerlMonk =cut use strict; use Term::ReadKey; use Time::HiRes qw/sleep/; use IO::Handle; STDOUT->autoflush(1); print "Input:"; my $passcode = getPassword(); print "\nYour input:", $passcode; PAUSE(); sub getPassword { my $end = 0; my $key; my $buff = ""; ReadMode 4; while ( 1 ) { while (not defined ($key = ReadKey(-1) )) { sleep 0.01; } if ( $key eq chr(13) ) { last; } elsif ( $key eq "\b" ) { reduce(\$buff); } else { $buff .= $key; print "*"; } } ReadMode 0; return $buff; } # 退格处理 sub reduce { my ($s_ref) = shift; my $len = length( $$s_ref ); return if ( $len <= 0 ); $$s_ref = substr( $$s_ref, 0, length($$s_ref)-1 ); print "\b \b"; } # Press any key to exit ... sub PAUSE { while ( ! ReadKey(-1) ) { sleep 0.01 } exit; }
输入输出示例
Input:********** Your input:abcdeftest
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 3 访客