perl – 如何解析命令行参数?

前端之家收集整理的这篇文章主要介绍了perl – 如何解析命令行参数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想解析perl脚本中的参数列表,例如我遇到这种情况:

script.pl -h 127.0.0.1 -u user -p pass arg1 arg2 arg3

我该如何解析数组中不是选项的参数列表,以及标量值中的选项参数?

谢谢.

解决方法

好吧,如果它们是命令行中唯一没有作为选项提供的东西,那么它们仍然应该在@ARGV中.所以只需使用@ARGV.

use Getopt::Long;

# save arguments following -h or --host in the scalar $host
# the '=s' means that an argument follows the option
# they can follow by a space or '=' ( --host=127.0.0.1 )
GetOptions( 'host=s' => \my $host,'user=s' => \my $user  # same for --user or -u,'pass=s' => \my $pass  # same for --pass or -p
          );

# @ARGV: [ qw<arg1 arg2 arg3> ]

Getopt::Long

猜你在找的Perl相关文章