LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1832|回复: 1

PromptUtil.pm源代码

[复制链接]
发表于 2003-12-1 21:52:26 | 显示全部楼层 |阅读模式
本模块导出了两个函数。get_passwd()函数用于提示用户密码输入,他关闭了回显因此用户的响应不能被看到。如果提供了一个用户名和一个主机,提示将会包含信息(当登陆到一个远程主机上的一个特定帐号时使用)。
prompt()指定的文本字符串设为提示符,如果用户输入为空的话则返回默认的提示符。用户被提示输入一个提示符,函数返回其响应。作为一个特例,函数检查是否有"q"的存在,有的话表示用户想推出。

package PromptUtil;
# file PromptUtil.pm
# Chapters 8,9/Appendix A: This module is used to prompt the user for passwords
# with echo off.

use strict;
require Exporter;
eval "use Term::ReadKey";

use vars '@EXPORT','@ISA';
@EXPORT = qw(get_passwd prompt);
@ISA = 'Exporter';

my $stty_settings;  # save old TTY settings

sub get_passwd {
  my ($user,$host) = @_;
  print STDERR "$user\@$host "
    if $user && $host;
  print STDERR "password: ";
  echo ('off');
  chomp(my $pass = <>);
  echo ('on');
  print STDERR "\n";
  $pass;
}

# print a prompt
sub prompt {
  local($|) = 1;
  my $prompt  = shift;
  my $default = shift;
  print "$prompt ('q' to quit) [$default]: ";
  chomp(my $response = <>);
  exit 0 if $response eq 'q';
  return $response || $default;
}

sub echo {
  my $mode = shift;
  if (defined &ReadMode) {
    ReadMode( $mode eq 'off' ? 'noecho' : 'restore' );
  } else {
    if ($mode eq 'off') {
      chomp($stty_settings = `/usr/bin/stty -g`);
      system "/usr/bin/stty -echo </dev/tty";  
    } else {
      $stty_settings =~ /^([:\da-fA-F]+)$/;
      system "/usr/bin/stty $1 </dev/tty";
    }
  }
}

######从这里开始的以下的程序是什么意思,好象不是perl

1;

=head1 NAME  

PromptUtil - Prompt utilities

=head1 SYNOPSIS

  use PromptUtil;

  my $response = prompt('<n>ext, <p>revious, or <e>dit','n');
  my $pass     = get_passwd();

=head1 DESCRIPTION

This package exports two utilities that are handy for prompting for
user input.

=head1 EXPORTED FUNCTIONS

=over 4

=item $result = prompt($prompt,$default)

Prints the indicated C<$prompt> to and requests a line of input.  If
the user types "q" or "quit" returns false.  Otherwise returns the
input line (minus the newline).  If the user hits return without
typing anything, returns the default specified by C<$default>.

=item $password = get_passwd([$user,$host])

Turns off terminal echo and prompts the user to enter his password.
If C<$user> and C<$host> are provided, the prompt is in the format

jdoe@host.domain password:

otherwise the prompt is simply

password:

The function returns the password, or undef it the user typed return
without entering a password.

=back

If get_passwd() detects that the Term::ReadKey module is available, it
will attempt to use that.  Otherwise it will call the Unix stty
program, which will not be available on non-Unix systems.

=head1 SEE ALSO

L<Term::ReadKey>, L<perl>

=head1 AUTHOR

Lincoln Stein <lstein@cshl.org>

=head1 COPYRIGHT

Copyright (c) 2000 Lincoln Stein. All rights reserved. This program is
free software; you can redistribute it and/or modify it under the same
terms as Perl itself.

=cut
发表于 2003-12-1 22:18:02 | 显示全部楼层
那是perl格式的文档,俗称plain old documentation(pod)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表