【问题标题】:how to get user input and use that value in the script for Perl如何获取用户输入并在 Perl 脚本中使用该值
【发布时间】:2014-08-08 08:45:07
【问题描述】:

我需要做以下事情:

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,如果这可以从脚本中打印出一些cmets?

【问题讨论】:

    标签: perl user-input


    【解决方案1】:

    试试这个命令行选项和帮助 ..run as..代替我的参数放年龄、性别、地址任何你想要的。

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

    #!/usr/bin/perl
    
    
    use Getopt::Long;
    GetOptions ("value=s" => \$value,    # string
                  "cmd=s"   => \$cmd,      # string
                  "pass=s"  => \$pass,# string
                  "help"=> \$help ) 
    or die("Error in command line arguments\n");
    
    
    if ( $help ){
            print " this is a test module for help please bare with it ";
            exit;
    }
     print "$cmd,$pass\n";
    

    【讨论】:

      【解决方案2】:

      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 上,虽然它有一些非核心构建依赖项,但它的运行时依赖项是仅核心的,而且非常轻量级。

      【讨论】:

      • @user3751267 还有其他问题没有得到解答吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 2018-10-04
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      相关资源
      最近更新 更多