在调试器中运行Perl代码时,警告会导致堆栈跟踪:我可以阻止这种情况吗?

时间:2014-08-21 14:30:55

标签: perl debugging

如果我的代码中有warn个语句,并且我通常从命令行运行此代码,我会在控制台上看到警告消息。同样,如果我在Apache下运行此代码,我的警告消息会出现在Apache的错误日志中。

如果我在perl -d下运行相同的代码,每次执行警告时都会看到堆栈转储。当我的代码通过活跃的Smart::Comments时,会发生同样的事情。我可以对此做些什么吗?

示例:

  DB<8> 
Highway::eBookGenerator::build(/var/www/highway/app/Highway/eBookGenerator.pm:133):
133:        $self->{opts}->{debug} = '' unless $self->{opts}->{debug};
  DB<8> 
Highway::eBookGenerator::build(/var/www/highway/app/Highway/eBookGenerator.pm:135):
135:        warn "build IS RUNNING UNSTABLE" if $self->{opts}->{runningunstable};
  DB<8> 
build IS RUNNING UNSTABLE at /var/www/highway/app/Highway/eBookGenerator.pm line 135.
 at /var/www/highway/app/Highway/eBookGenerator.pm line 135.
    Highway::eBookGenerator::build('Highway::eBookGenerator=HASH(0xf2da4f0)', 'HASH(0xf1369e8)') called at /var/www/highway/app//Highway/Page/eBookViewer.pm line 2448
    Highway::Page::eBookViewer::create_epub_process('Highway::Page::eBookViewer=HASH(0xf35b038)') called at /var/www/highway/app/_cpan/CGI/Application.pm line 157
    eval {...} called at /var/www/highway/app/_cpan/CGI/Application.pm line 156
    CGI::Application::__get_body('Highway::Page::eBookViewer=HASH(0xf35b038)', 'create_epub_process') called at /var/www/highway/app/_cpan/CGI/Application.pm line 234
    .... yada yada 20 lines of stack dump
    Test::WWW::Mechanize::post_ok('Test::WWW::Mechanize::PSGI=HASH(0xea5d230)', '/ebooks/create_epub_process', 'HASH(0x159f8d0)') called at t/ebookGenTest.pl line 160
Highway::eBookGenerator::build(/var/www/highway/app/Highway/eBookGenerator.pm:140):

1 个答案:

答案 0 :(得分:1)

把:

$SIG{__WARN__} = sub { };
主源文件中的

应该捕获并因此抑制任何将来的警告。或者你可以这样做:

$SIG{__WARN__} = sub { print "$1\n" if $1 };

有一个非侵入性的消息。