如何设置Perl脚本,以便命令行上的输入表示脚本在哪个/哪个目录(什么目录)?Perl命令行输入?
例如:
cdm line:>perl text.pl C:/pathtodirectory/
在脚本中,我得到一个veriable $路径设置为:C:/ pathtodirectory
感谢
如何设置Perl脚本,以便命令行上的输入表示脚本在哪个/哪个目录(什么目录)?Perl命令行输入?
例如:
cdm line:>perl text.pl C:/pathtodirectory/
在脚本中,我得到一个veriable $路径设置为:C:/ pathtodirectory
感谢
你需要使用@ARGV
数组。
所以你text.pl
:
my $path = shift(@ARGV);
或
my $path = shift; # @ARGV is the default in the main part of your script
@ARGV
是从指数0开始在命令行上的每个项目......所以如果你有更多的选项,如
text.pl some/path some_other_option
some_other_option
将作为$ARGV[1]
对于更高级的路径处理,请查看Getopt::Std
或Getopt::Long
模块(它们应该在默认情况下包含在Perl中。
的命令行参数都放在数组@ARGV。你可以得到它们:
my $path = shift @ARGV;
# or just (shift defaults to using @ARGV outside of any function):
my $path = shift;
谢谢!非常有帮助! – dewalla 2011-03-14 18:47:46
@ARGV
预定义变量包含脚本的命令行参数。
在这种情况下,您可以使用perl的Getopt::Long
模块documented here。
或者你可以简单解析ARGV
:
$ perl -MData::Dumper -e 'print Dumper \@ARGV;' foo bar
几乎总是一个好主意,从Getopt :: Long开始,而不是:: Std。使用它并不复杂,当你需要它的时候会做。 – ysth 2011-03-14 18:40:53
谢谢!非常有帮助! – dewalla 2011-03-14 18:47:16
您可能还想查看Getopt :: Euclid,它处理参数并从文档中获取它的规则。 – 2011-03-15 00:45:44