gpt4 book ai didi

perl "or"错误处理 : multi-statement on error possible?

转载 作者:行者123 更新时间:2023-12-04 10:01:25 26 4
gpt4 key购买 nike

这个结构在 perl 中很常见:

opendir (B,"/somedir") or die "couldn't open dir!";

但这似乎不起作用:
opendir ( B, "/does-not-exist " ) or {
print "sorry, that directory doesn't exist.\n";
print "now I eat fugu.\n";
exit 1;
};

“或”错误处理是否可能有多个命令?

编译以上内容:
# 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.

最佳答案

您可以随时使用 do :

opendir ( B, "/does-not-exist " ) or do {
print "sorry, that directory doesn't exist.\n";
print "now I eat fugu.\n";
exit 1;
}

或者您可以使用 if/unless:
unless (opendir ( B, "/does-not-exist " )) {
print "sorry, that directory doesn't exist.\n";
print "now I eat fugu.\n";
exit 1;
}

或者你可以一起摆动你自己的子程序:
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;
}

有不止一种方法可以做到这一点。

关于perl "or"错误处理 : multi-statement on error possible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10454061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com