如何获取用户输入并在Perl脚本中使用该值

如何获取用户输入并在Perl脚本中使用该值,perl,user-input,Perl,User Input,我需要做以下工作: Print "User please enter the age, sex, blah_blah" > $age>$sex>$blah_blah; print "the current user stats are- age = $age, sex = $sex"; my $rand; if ($blah_blah > $rand) { do something } else {

我需要做以下工作:

Print "User please enter the age, sex, blah_blah" > $age>$sex>$blah_blah;


print "the current user stats are- age = $age, sex = $sex";

my $rand;

if ($blah_blah > $rand)

      {

      do something

      }

else 

      {
      something else
      }
有人能帮我从用户那里获取输入,然后在脚本中使用该值吗

我还希望能够以以下方式运行此脚本:

perl script1 -age -sex -blah_blah
我的意思是,我可以在命令行中简单地键入这些值,而不是要求用户提供这些值,并且我的脚本仍然可以如上所述使用这些值吗


这可能吗?如果是这样,我还可以添加-help,即perl script1-help,以及它是否可以打印脚本中的一些注释?

请尝试使用此命令行选项和帮助..运行方式..代替我的参数,输入年龄、性别和地址

perl test.pl-pass'abcd'-cmd'abcd'-path'paths'-value'testde'


Perl模块IO::Prompt::Tiny用于使用提示消息进行提示、可移植地接受输入,或者在检测到非交互式运行时时接受默认值

将其与Getopt::Long结合使用以接受命令行输入

您的程序可能会检查Getopt::Long选项的定义,对于命令行中未提供的每个选项,调用提示符

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和是核心Perl模块;它们随每个Perl发行版一起提供,因此您应该已经有了它们。在CPAN上,尽管它有一些非核心构建依赖项,但它的运行时依赖项仅为核心,而且非常轻。

@user3751267是否还有其他问题没有解决?
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