【问题标题】:warnings::warnif( 'deprecated' ... ) with carp?warnings::warnif( 'deprecated' ... ) with carp?
【发布时间】:2013-08-01 08:26:25
【问题描述】:

我想发送类似的弃用通知

warnings::warnif( 'deprecated', 'function foo is deprecated' );

但我更愿意让它报告给调用者,而不是实际警告的位置。我可以用carp 以某种方式做到这一点吗?

【问题讨论】:

    标签: perl


    【解决方案1】:

    你有tried it?

    package Foo {
        sub bar {
            warnings::warnif(deprecated => 'Foo:bar is deprecated');
        }
    }
    
    use warnings;
    # no warnings 'deprecated';    # <-- uncomment this to disable the warning
    Foo::bar();                    # <-- this is line 9
    

    这应该是这样的:

    Foo::bar is deprecated at test.pl line 9.
    

    其实看warnings.pm source,内部好像使用了Carp.pm。诚然,警告编译指示本身的文档可能对此更清楚,但perllexwarn 确实很清楚这就是它的用途。

    【讨论】:

    • 是的,我试过了.. MOP 正在丢弃我所有的行号......显然即使现在我已经适当地设置了@CARP_NOT,所以我没有意识到它做对了事情(或者最初 @CARP_NOT 会被服从)。现在我想我需要弄清楚自从我将__PACKAGE__ 放入@CARP_NOT 以来我缺少什么,但它似乎仍然是从包而不是测试文件中吹毛求疵。
    猜你喜欢
    • 2022-12-22
    • 2011-02-03
    • 1970-01-01
    • 2018-03-20
    • 2016-02-28
    • 2020-06-05
    • 2017-04-09
    • 2012-05-26
    • 2012-05-27
    相关资源
    最近更新 更多