Perl 模块 IO::Prompt::Tiny 可用于使用提示消息进行提示、可移植地接受输入或在检测到非交互式运行时接受默认值。
将此与 Getopt::Long 结合使用以接受命令行输入。
您的程序可能会检查 Getopt::Long 选项的定义性,对于没有在命令行中提供的每个选项,调用 prompt()。
Perl 模块 Pod::Usage 使使用信息和文档变得微不足道。
这是一个例子:
use strict;
use warnings;
use IO::Prompt::Tiny 'prompt';
use Pod::Usage;
use Getopt::Long;
my( $age, $sex, $blah, $help, $man );
GetOptions(
'age=s' => \$age,
'sex=s' => \$sex,
'blah=s' => \$blah,
'help|?' => \$help,
'man' => \$man,
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-exitval => 0, -verbose => 2) if $man;
$age //= prompt( 'Enter age: ', '0' );
$sex //= prompt( 'Enter sex: ', 'undeclared' );
$blah //= prompt( 'Blab now: ', '' );
print "Your age was entered as <$age>, sex declared as <$sex>, and you ",
"chose to blab about <$blah>.\n";
__END__
=head1 User Input Test
sample.pl - Using Getopt::Long, Pod::Usage, and IO::Prompt::Tiny
=head1 SYNOPSIS
sample.pl [options]
Options:
-help Brief help message.
-age n Specify age.
-sex s Specify sex.
-blah blah Blab away.
=head1 INTERACTIVE MODE
If C<-age>, C<-sex>, and C<-blah> are not supplied on the command line, the
user will be prompted interactively for missing parameters.
=cut
Getopt::Long 和Pod::Usage 是核心Perl 模块;它们随每个 Perl 发行版一起提供,因此您应该已经拥有它们。 IO::Prompt::Tiny 在 CPAN 上,虽然它有一些非核心构建依赖项,但它的运行时依赖项是仅核心的,而且非常轻量级。