2011-03-14 153 views
1

如何设置Perl脚本,以便命令行上的输入表示脚本在哪个/哪个目录(什么目录)?Perl命令行输入?

例如:

cdm line:>perl text.pl C:/pathtodirectory/ 

在脚本中,我得到一个veriable $路径设置为:C:/ pathtodirectory

感谢

回答

4

你需要使用@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::StdGetopt::Long模块(它们应该在默认情况下包含在Perl中。

+0

几乎总是一个好主意,从Getopt :: Long开始,而不是:: Std。使用它并不复杂,当你需要它的时候会做。 – ysth 2011-03-14 18:40:53

+0

谢谢!非常有帮助! – dewalla 2011-03-14 18:47:16

+0

您可能还想查看Getopt :: Euclid,它处理参数并从文档中获取它的规则。 – 2011-03-15 00:45:44

4

的命令行参数都放在数组@ARGV。你可以得到它们:

my $path = shift @ARGV; 
# or just (shift defaults to using @ARGV outside of any function): 
my $path = shift; 
+0

谢谢!非常有帮助! – dewalla 2011-03-14 18:47:46

1

@ARGV预定义变量包含脚本的命令行参数。

1

在这种情况下,您可以使用perl的Getopt::Long模块documented here

或者你可以简单解析ARGV

$ perl -MData::Dumper -e 'print Dumper \@ARGV;' foo bar