如何使用块作为“或” 子句而不是简单的骰子?
我想检查 Net::FTP Perl 模块中的操作结果而不是死掉。
通常你会这样做:
$ftp->put($my_file)
or die "Couldn't upload file";
但我想做点别的事情,而不是仅仅死在这个脚本中,所以我尝试了:
$ftp->put($my_file)
or {
log("Couldn't upload $my_file");
return(-1);
}
log("$my_file uploaded");
但 Perl 抱怨编译错误说:
syntax error at toto.pl line nnn, near "log"
这是我的代码片段中的第二个日志。
任何建议都非常感激。
干杯,
I want to check the results of an operation in the Net::FTP Perl module rather than die.
Typically you would do:
$ftp->put($my_file)
or die "Couldn't upload file";
But I want to do something else instead of just dying in this script so I tried:
$ftp->put($my_file)
or {
log("Couldn't upload $my_file");
return(-1);
}
log("$my_file uploaded");
But Perl complains of compilation errors saying:
syntax error at toto.pl line nnn, near "log"
which is the second log in my code fragment.
Any suggestions greatly appreciated.
cheers,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
do
就是你的正在寻找:但是这可能是更好的风格:
如果您只想返回错误条件,那么您可以
die
并使用 < code>eval 在调用函数中。然后调用
}
do
is what you're looking for:But this is probably better style:
If you just want to return an error condition, then you can
die
and useeval
in the calling func.and then call
}
或执行{}; 总是让我头疼。 使用“or”语法(我承认在单行中使用了很多)与“if”(我更喜欢在多行中使用)是否有充分的理由?
那么,是否有理由使用或不使用其中一种方法而不是另一种呢?
or do{}; always makes my head hurt. Is there a good reason to use "or" syntax (which I admit using a lot for one liners) vs "if" (which I prefer for multi liners)?
So, is there a reason to use or not use one of these methods in preference of the other?
使用do。
这是小代码片段:
use do.
here is small code snippet:
我很难理解为什么这需要包含在一个 do 中。 是否有理由认为这还不够?
这假设 put 函数并不总是在成功时返回 undef。
I'm having a hard time understanding why this needs to be wrapped up in a do. Is there a reason that this isn't sufficient?
This assumes that the put function doesn't always return undef on success.