Perl warning printed without 'use warnings' or -w in any files

后端 未结 1 1592
情话喂你
情话喂你 2021-01-24 05:51

I have a lot of old Perl code that gets called frequently, I have been writing a new module and all of a sudden I\'m getting a lot of warnings in my error_log for Apache, they a

相关标签:
1条回答
  • 2021-01-24 06:16

    I take it that the code gets called by running certain top-level Perl script(s).

    Then use the __WARN__ hook in those script(s) to stop printing of warnings

    BEGIN { $SIG{__WARN__} = sub {} };
    

    Place this BEGIN block before the use statements so to affect modules as well.

    An empty subroutine is the way to mute warnings since __WARN__ doesn't support 'IGNORE'.

    See warn and %SIG in perlvar. See this post and this post for comments and some examples.


    To investigate further and track the warnings you can use Carp

    BEGIN {
        $SIG{__WARN__} = \&Carp::cluck;  # or Carp::confess; to also die
    }
    

    which will make it print full stack traces. This can be fine-tuned as you please since we can write our own sub to be called. Or use Carp::Always.

    See this post for some more drastic measures (like overriding CORE::GLOBAL::warn)

    Once you find a more precise level at which to suppress warnings then local $SIG{__WARN__} is the way to go, if possible. This is used in a post linked above, and here is another example. It is of course far better to suppress warnings only where needed instead of everywhere.

    More detail

    • Getting stack traces in Perl?

    • How can I get a call stack listing in Perl?

    Note that longmess is unfortunately no longer so standard and well supported.

    0 讨论(0)
提交回复
热议问题