我正在使用unix终端的消息系统上工作,因此为了使消息输出更加用户友好,我想在按Enter按钮以将其用于其他消息输出后隐藏<STDIN>
输入。
my $user = "Someone";
my $message = <STDIN>; #must show what does user type but should hide the message after pressing enter
chomp $message;
print messagefile "<$user> $message\n";
我已经在论坛上阅读到某种方法正在使用
Term::ReadKey
,但不幸的是,由于该模块未出现在系统中,因此我无法执行该操作。 最佳答案
借用from answer。它一次读取一个字符,然后按Enter键,它会用\r <spaces> \r
擦除当前行
use strict;
use warnings;
sub get_pass {
local $| = 1;
my $ret = "";
while (1) {
my $got = getone();
last if $got eq "\n";
print $got;
$ret .= $got;
}
print "\r", " " x length($ret), "\r";
return $ret;
}
my $user = "Someone";
my $message = get_pass();
chomp $message;
print "<$user> $message\n";
BEGIN {
use POSIX qw(:termios_h);
my ($term, $oterm, $echo, $noecho, $fd_stdin);
$fd_stdin = fileno(STDIN);
$term = POSIX::Termios->new();
$term->getattr($fd_stdin);
$oterm = $term->getlflag();
$echo = ECHO | ECHOK | ICANON;
$noecho = $oterm & ~$echo;
sub cbreak {
$term->setlflag($noecho);
$term->setcc(VTIME, 1);
$term->setattr($fd_stdin, TCSANOW);
}
sub cooked {
$term->setlflag($oterm);
$term->setcc(VTIME, 0);
$term->setattr($fd_stdin, TCSANOW);
}
sub getone {
my $key = '';
cbreak();
sysread(STDIN, $key, 1);
cooked();
return $key;
}
}
END { cooked() }