Perl command line input?
How do I setup a Perl开发者_StackOverflow社区 script so that the input on the command line denotes what/where the script looks at (what directory)?
example:
cdm line:>perl text.pl C:/pathtodirectory/
In the script I get a veriable $path to be set to: C:/pathtodirectory
Thanks
You need to use the @ARGV
array.
So in your 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
text.pl some/path some_other_option
some_other_option
would be available as $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.
Command line arguments are placed in the array @ARGV. You can get them like:
my $path = shift @ARGV;
# or just (shift defaults to using @ARGV outside of any function):
my $path = shift;
The @ARGV
predefined variable contains the command-line arguments to the script.
In this case you can use perl's Getopt::Long
module documented here.
Or you can simply parse ARGV
:
$ perl -MData::Dumper -e 'print Dumper \@ARGV;' foo bar
精彩评论