Perl Tricks

From Athenaeum
Jump to: navigation, search


Terminal IO

Get a secret string

Retrieving a password (or any secret text) from the command line in perl:

sub get_secret {
  my $pass;
  Term::ReadKey::ReadMode(2);  # turn off echo
  while ($pass = ReadLine(0)) {last if defined $pass;}
  chomp($pass);  # Remove trailing newline
  print "\n";
  Term::ReadKey::ReadMode(0);  # turn on echo
  return $pass;
}