按Enter后隐藏STDIN echo

时间:2021-12-18 07:26:38

I'm working on a message system that uses unix terminal, so to make message output more user friendly, I wanted to hide <STDIN> input after pressing enter button to use it in another message output.

我正在开发一个使用unix终端的消息系统,为了使消息输出更方便用户,我想在按下enter按钮后将 输入隐藏到另一个消息输出中。

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";

I've read in forums that some method is using Term::ReadKey but unfortunately I'm not able to do that since that module does not present in the system.

我在论坛上读到一些方法正在使用Term::ReadKey,但不幸的是,我不能这样做,因为该模块没有出现在系统中。

2 个解决方案

#1


2  

Borrowed from answer. It reads one character at time, and when enter is pressed, it wipes current line with \r <spaces> \r

借用了答案。它每次读取一个字符,当输入被按下时,它用\r \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() }

#2


1  

From http://www.perlmonks.org/?node_id=33353

从http://www.perlmonks.org/?node_id=33353

use autodie qw(:all);

print "login: ";
my $login = <>;
print "Password: ";
system('stty', '-echo');  # Disable echoing
my $password = <>;
system('stty', 'echo');   # Turn it back on

#1


2  

Borrowed from answer. It reads one character at time, and when enter is pressed, it wipes current line with \r <spaces> \r

借用了答案。它每次读取一个字符,当输入被按下时,它用\r \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() }

#2


1  

From http://www.perlmonks.org/?node_id=33353

从http://www.perlmonks.org/?node_id=33353

use autodie qw(:all);

print "login: ";
my $login = <>;
print "Password: ";
system('stty', '-echo');  # Disable echoing
my $password = <>;
system('stty', 'echo');   # Turn it back on