3

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.

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.

user3544092
  • 333
  • 2
  • 4
  • 13
  • 1
    install the missing module with CPAN, or cpanm Term::ReadKey, if you have apt-get you can try apt-get install libterm-readkey-perl – Zach Leighton Oct 08 '14 at 14:08
  • I would if i could, I have no permission and I'm not able to ask administration. I'm searching for another way – user3544092 Oct 08 '14 at 14:08
  • 3
    [You don't need root to install modules](http://stackoverflow.com/questions/3735836/how-can-i-install-perl-modules-without-root-privileges). – ThisSuitIsBlackNot Oct 08 '14 at 14:40
  • Just out of curiosity, how would hiding what you just typed make a messaging system more user friendly? Wouldn't you want to see a record of what's been said? – ThisSuitIsBlackNot Oct 08 '14 at 14:48
  • 2
    I'm not sure what "I'm not able to ask administration" means. Are you banned from talking to them? But seriously, if you're in a Perl environment where you think you can't install modules (and, by the way, [that's never the case](http://shadow.cat/blog/matt-s-trout/but-i-cant-use-cpan/)) then that's a major impediment to your Perl programming and you should get that problem fixed before any others. – Dave Cross Oct 08 '14 at 14:54

2 Answers2

2

Borrowed from answer. It reads one character at time, and when enter is pressed, it wipes current line with \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() }
Community
  • 1
  • 1
mpapec
  • 50,217
  • 8
  • 67
  • 127
1

From 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
mpapec
  • 50,217
  • 8
  • 67
  • 127
Zach Leighton
  • 1,939
  • 14
  • 24