如何使用perl从命令行将目录路径作为参数传递?
我的问题如下:如何使用perl从命令行将目录路径作为参数传递?,perl,Perl,我的问题如下: ./welcome.pl -output_dir "/home/data/output" 我突然想到了如何传递命令行参数,而不是使用perl传递目录路径 示例假设我正在执行文件,如下所示: ./welcome.pl -output_dir "/home/data/output" 我的代码: #!/usr/local/bin/perl use strict; use warnings 'all'; use Getopt::Long 'GetOptions'; GetOpti
./welcome.pl -output_dir "/home/data/output"
我突然想到了如何传递命令行参数,而不是使用perl传递目录路径
示例假设我正在执行文件,如下所示:
./welcome.pl -output_dir "/home/data/output"
我的代码:
#!/usr/local/bin/perl
use strict;
use warnings 'all';
use Getopt::Long 'GetOptions';
GetOptions(
'output=s' => \my $output_dir,
);
my $location_dir="/home/data/output";
print $location_dir;
代码说明:
我试图打印$output\u dir中的内容,因此我需要在变量(即$location\u dir
)中传递命令行参数,而不是直接传递路径。我该怎么做
use strict;
use warnings 'all';
use File::Basename qw( basename );
use Getopt::Long qw( GetOptions );
sub usage {
if (@_) {
my ($msg) = @_;
chomp($msg);
print(STDERR "$msg\n");
}
my $prog = basename($0);
print(STDERR "$prog --help for usage\n");
exit(1);
}
sub help {
my $prog = basename($0);
print(STDERR "$prog [options] --output output_dir\n");
print(STDERR "$prog --help\n");
exit(0);
}
Getopt::Long::Configure(qw( posix_default )); # Optional, but makes the argument-handling consistent with other programs.
GetOptions(
'help|h|?' => \&help,
'output=s' => \my $location_dir,
)
or usage();
defined($location_dir)
or usage("--output option is required\n");
print("$location_dir\n");
当然,如果参数确实是必需的,那么为什么不直接使用/welcome.pl”/home/data/output“
,而不是真正的可选参数呢
use strict;
use warnings 'all';
use File::Basename qw( basename );
use Getopt::Long qw( GetOptions );
sub usage {
if (@_) {
my ($msg) = @_;
chomp($msg);
print(STDERR "$msg\n");
}
my $prog = basename($0);
print(STDERR "$prog --help for usage\n");
exit(1);
}
sub help {
my $prog = basename($0);
print(STDERR "$prog [options] [--] output_dir\n");
print(STDERR "$prog --help\n");
exit(0);
}
Getopt::Long::Configure(qw( posix_default )); # Optional, but makes the argument-handling consistent with other programs.
GetOptions(
'help|h|?' => \&help,
)
or usage();
@ARGV == 1
or usage("Incorrect number of arguments\n");
my ($location_dir) = @ARGV;
print("$location_dir\n");
当然,如果参数确实是必需的,那么为什么不直接使用/welcome.pl”/home/data/output“
,而不是真正的可选参数呢
use strict;
use warnings 'all';
use File::Basename qw( basename );
use Getopt::Long qw( GetOptions );
sub usage {
if (@_) {
my ($msg) = @_;
chomp($msg);
print(STDERR "$msg\n");
}
my $prog = basename($0);
print(STDERR "$prog --help for usage\n");
exit(1);
}
sub help {
my $prog = basename($0);
print(STDERR "$prog [options] [--] output_dir\n");
print(STDERR "$prog --help\n");
exit(0);
}
Getopt::Long::Configure(qw( posix_default )); # Optional, but makes the argument-handling consistent with other programs.
GetOptions(
'help|h|?' => \&help,
)
or usage();
@ARGV == 1
or usage("Incorrect number of arguments\n");
my ($location_dir) = @ARGV;
print("$location_dir\n");
你能解释一下为什么你不必使用帮助选项@IKEGAMIDI吗。但我觉得这比将
打印(STDERR“询问用户”在StackOverflow上查找数据以供使用\n)要好代码>在用法()
:)你能解释一下为什么你不必使用帮助选项@ikegamiddn吗。但我觉得这比将打印(STDERR“询问用户”在StackOverflow上查找数据以供使用\n)要好代码>在用法()中(
:)