如何获取用户输入并在 Perl 脚本中使用该值 [英] how to get user input and use that value in the script for Perl

查看:40
本文介绍了如何获取用户输入并在 Perl 脚本中使用该值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行以下操作:

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
      }

有人可以帮助我从用户那里获取输入,然后能够在脚本中使用该值吗?

Can someone help me with to take inputs from the user and then be able to use that value in the script?

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

I also want to be able to run this script as:

perl script1 -age -sex -blah_blah

我的意思是,我可以简单地在命令行中输入这些值而不是要求用户提供这些值,并且我的脚本是否仍然可以像上面一样使用这些值?

By that I mean instead of asking the user to provide these values can I simply type these in the command line and can my script still use these as above?

这可能吗?如果是这样,我是否还可以添加 -helpperl script1 -help,如果这可以从脚本中打印出一些注释?

Is this possible? If so, can I also add -help i.e. perl script1 -help and if this could print out some comments from the script?

推荐答案

试试这个命令行选项和帮助 ..run as..in place of my params put age,sex,address 无论你想要什么.

Try this for command line options and help ..run as..in place of my params put age,sex,address whatever you want.

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";

这篇关于如何获取用户输入并在 Perl 脚本中使用该值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆