在Perl中“回复警告”是一种好习惯吗?

前端之家收集整理的这篇文章主要介绍了在Perl中“回复警告”是一种好习惯吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想检查一个条件,打印一个警告,并从一个子程序返回一行代码.这有效(我认为警告返回1):

return warn "can't find file" if not -f $input_file;

我可以安全地这样做吗?或者还有更好的方法吗?

解决方法

这是非常安全的,但它需要查看 source来确定返回的值(true),并且没有留下可读的选项来控制返回的值.这是非常重要的,因为在这种情况下你通常想要返回/ undef / false,但是你现在返回true.

以下所有备选方案都允许您指定返回的值:

warn("can't find file"),return if !-f $input_file;

(warn "can't find file"),return if !-f $input_file;

if (!-f $input_file) { warn "can't find file"; return }

-f $input_file or warn("can't find file"),return;

-f $input_file or (warn "can't find file"),return;

猜你在找的Perl相关文章