旧行李 2008-12-12 03:05 采纳率: 25%
浏览 552
已采纳

如何将命令行参数传递给 Perl 程序?

I'm working on a Perl script. How can I pass command line parameters to it?

Example:

script.pl "string1" "string2"

转载于:https://stackoverflow.com/questions/361752/how-can-i-pass-command-line-arguments-to-a-perl-program

  • 写回答

9条回答 默认 最新

  • Didn"t forge 2008-12-12 03:15
    关注

    Depends on what you want to do. If you want to use the two arguments as input files, you can just pass them in and then use <> to read their contents.

    If they have a different meaning, you can use GetOpt::Std and GetOpt::Long to process them easily. GetOpt::Std supports only single-character switches and GetOpt::Long is much more flexible. From GetOpt::Long:

    use Getopt::Long;
    my $data   = "file.dat";
    my $length = 24;
    my $verbose;
    $result = GetOptions ("length=i" => \$length,    # numeric
                        "file=s"   => \$data,      # string
                        "verbose"  => \$verbose);  # flag
    

    Alternatively, @ARGV is a special variable that contains all the command line arguments. $ARGV[0] is the first (ie. "string1" in your case) and $ARGV[1] is the second argument. You don't need a special module to access @ARGV.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥15 mysql全文索引查找指定必须关键词word无效
  • ¥20 关于CMOS电路图的提问
  • ¥15 Verilog hdl密码锁设计
  • ¥35 基于python的有ssl加密传输的socket聊天室
  • ¥15 数码管亮度控制器设计
  • ¥15 kafka客户端跨网段访问,看日志提示连接的还剩内网地址,且访问不通
  • ¥15 关于c语言代码的问题
  • ¥15 c51单片机控制步进电机
  • ¥20 Visual studio无法检测到设备
  • ¥30 vue 页面窗口放大或者缩小元素会变化