简体   繁体   English

Python的警告模块的Perl等价习惯用法

[英]Perl equivalent idiom for Python's warnings module

I need to port some code from Python to Perl. 我需要将一些代码从Python移植到Perl。 The Python code makes simple use of the warnings module, eg Python代码简单地使用了警告模块,例如

warnings.warn("Hey I'm a warning.")

I've been googling around quite a bit but it's unclear to me what the equivalent Perl idiom might be. 我一直在谷歌搜索相当多,但我不清楚相同的Perl成语可能是什么。 How would a Perl programmer handle this? Perl程序员将如何处理这个问题?

To write a message to STDERR , simply use the built-in warn function. 要将消息写入STDERR ,只需使用内置warn功能。

warn "Hey I'm a warning.";

But you should also use Perl's warnings module, as well as strict because they turn on all kinds of useful compiler warnings and error checking for you. 但是你应该使用Perl的warnings模块,以及strict因为它们会为你打开各种有用的编译器警告和错误检查。

Therefore, begin all your programs with 因此,开始所有的程序

use strict;
use warnings;

warn "Hey I'm a warning.";

(You don't need the warnings module to use the warn function, though.) (但是,您不需要warnings模块来使用warn功能。)

If you want something more in-depth than the simple warn function, you can use the Carp module. 如果您想要比简单warn功能更深入的东西,可以使用Carp模块。 One of the particularly nice things about it is that it will let you print stacktraces with either warnings or errors. 关于它的一个特别好的事情是它可以让你打印带有警告或错误的堆栈跟踪。 Full documentation available on the Perl site . Perl站点上提供完整文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM