当我传递无效的文件名时,为什么这个脚本会继续运行而不会死?
#!/usr/bin/env perl
use warnings;
use 5.12.0;
use Getopt::Long qw(GetOptions :config bundling);
sub help {
say "no help text";
}
sub read_config {
my $file = shift;
open my $fh, '<', $file or die $!;
say <$fh>;
close $fh;
}
sub helpers_dispatch_table {
return {
'h_help' => \&help,
'r_read' => \&read_config,
};
}
my $file_conf = 'does_not_exist.txt';
my $helpers = helpers_dispatch_table();
GetOptions (
'h|help' => sub { $helpers->{h_help}(); exit; },
'r|read' => sub { $helpers->{r_read}( $file_conf ); exit },
);
say "Hello, World!\n" x 5;
答案 0 :(得分:4)
来自 perldoc Getopt :: Long
A trivial application of this mechanism is to implement options that are related to each other. For example:
my $verbose = ''; # option variable with default value (false)
GetOptions ('verbose' => \$verbose,
'quiet' => sub { $verbose = 0 });
Here "--verbose" and "--quiet" control the same variable $verbose, but with opposite values.
If the subroutine needs to signal an error, it should call die() with the desired error message as its argument. GetOptions() will catch the die(),
issue the error message, and record that an error result must be returned upon completion.
答案 1 :(得分:1)
问题源于您尝试混合两个任务,而不是检查GetOptions是否发现任何错误。
实际上,修复任何一个错误都可以解决你的问题,但是这里有两个方法:
sub help {
# Show usage
exit(0);
}
sub usage {
my $tool = basename($0);
print(STDERR "$_[0]\n") if @_;
print(STDERR "Usage: $tool [OPTIONS] FILE ...\n");
print(STDERR "Try `$tool --help' for more information.\n");
exit(1);
}
my $opt_read;
GetOptions (
'h|help' => \&help,
'r|read' => \$opt_read,
) or usage("Invalid arguments");
my $config = read_config($opt_read);