这个结构在perl中很常见:
opendir (B,"/somedir") or die "couldn't open dir!";
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用:
opendir ( B, "/does-not-exist " ) or {
print "sorry, that directory doesn't exist.\n";
print "now I eat fugu.\n";
exit 1;
};
Run Code Online (Sandbox Code Playgroud)
"或"错误处理是否有可能有多个命令?
编译以上内容:
# perl -c test.pl
syntax error at test.pl line 5, near "print"
syntax error at test.pl line 7, near "}"
test.pl had compilation errors.
Run Code Online (Sandbox Code Playgroud)
TLP*_*TLP 19
你可以随时使用do:
opendir ( B, "/does-not-exist " ) or do {
print "sorry, that directory doesn't exist.\n";
print "now I eat fugu.\n";
exit 1;
}
Run Code Online (Sandbox Code Playgroud)
或者您可以使用if/unless:
unless (opendir ( B, "/does-not-exist " )) {
print "sorry, that directory doesn't exist.\n";
print "now I eat fugu.\n";
exit 1;
}
Run Code Online (Sandbox Code Playgroud)
或者你可以把你自己的子程序放在一起:
opendir ( B, "/does-not-exist " ) or fugu();
sub fugu {
print "sorry, that directory doesn't exist.\n";
print "now I eat fugu.\n";
exit 1;
}
Run Code Online (Sandbox Code Playgroud)
有不止一种方法可以做到这一点.