[英]Perl command line input?
How do I setup a Perl script so that the input on the command line denotes what/where the script looks at (what directory)? 如何设置Perl脚本,以便在命令行上的输入指示脚本的目标/位置(哪个目录)?
example: 例:
cdm line:>perl text.pl C:/pathtodirectory/
In the script I get a veriable $path to be set to: C:/pathtodirectory 在脚本中,我得到一个可验证的$ path设置为: C:/ pathtodirectory
Thanks 谢谢
Command line arguments are placed in the array @ARGV. 命令行参数放置在数组@ARGV中。 You can get them like:
您可以像这样获得它们:
my $path = shift @ARGV;
# or just (shift defaults to using @ARGV outside of any function):
my $path = shift;
You need to use the @ARGV
array. 您需要使用
@ARGV
数组。
So in your text.pl
: 所以在您的
text.pl
:
my $path = shift(@ARGV);
or 要么
my $path = shift; # @ARGV is the default in the main part of your script
@ARGV
is each item on the command line starting from index 0 ... so if you had more options like @ARGV
是命令行中从索引0开始的每个项目...因此,如果您有更多选择,例如
text.pl some/path some_other_option
some_other_option
would be available as $ARGV[1]
some_other_option
将作为$ARGV[1]
For more advanced path processing take a look at the Getopt::Std
or Getopt::Long
modules (they should be included with Perl by default. 有关更高级的路径处理,请查看
Getopt::Std
或Getopt::Long
模块(默认情况下,Perl中应包含它们)。
@ARGV
预定义变量包含脚本的命令行参数。
In this case you can use perl's Getopt::Long
module documented here . 在这种情况下,可以使用此处记录的 perl的
Getopt::Long
模块。
Or you can simply parse ARGV
: 或者您可以简单地解析
ARGV
:
$ perl -MData::Dumper -e 'print Dumper \@ARGV;' foo bar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.