perl代码 -
#!/usr/bin/perl
package poc::template;
use strict;
use warnings;
use Log::Log4perl;
my $log_conf = "../conf/log4perl.conf";
Log::Log4perl::init($log_conf);
my $logger = Log::Log4perl->get_logger();
sub doSome {
$logger->info('doing something');
my $add = $ARGV[0] + 4;
}
#main
$logger->info('start');
eval { doSome(); };
if ($@) {
$logger->info('error');
}
$logger->info('stop');
这是log conf -
log4perl.rootLogger = DEBUG, SCREEN
log4perl.appender.SCREEN = Log::Log4perl::Appender::Screen
log4perl.appender.SCREEN.stderr = 0
log4perl.appender.SCREEN.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.SCREEN.layout.ConversionPattern = %d [%M:%L] %p %F{2} - %m%n
这是脚本输出 -
2013/02/03 16:47:02 [main:::18] INFO template\POCTemplate.pl - start
Use of uninitialized value $ARGV[0] in addition (+) at /POCPerl/template/POCTemplate.pl line 14.
2013/02/03 16:47:02 [poc::template::doSome:13] INFO template\POCTemplate.pl - doing something
2013/02/03 16:47:02 [main:::23] INFO template\POCTemplate.pl - stop
这个eval块不应该捕获该异常吗?
答案 0 :(得分:8)
不,这是一个警告,不是例外。
你可以这样做:
use warnings 'FATAL' => 'all';
使所有perl生成的警告异常(在给定的词法范围内)。