http://qs321.pair.com?node_id=868134

saberworks has asked for the wisdom of the Perl Monks concerning the following question:

I have been beating my head against this for a bit and I feel like I'm fundamentally misunderstanding how eval {} and $@ work. Given the following script with the following following log4perl configuration, I would expect that $@ wouldn't be unset in the middle of the if($@) block. What I'm seeing is that if I call any loglevel method on the $logger object it clears $@. Is this expected behavior?
#!perl use warnings; use strict; use Log::Log4perl; Log::Log4perl->init_and_watch('yourlogger.conf', 10); my $logger = Log::Log4perl::get_logger('test_die'); $logger->warn("before eval"); eval { die 'foo'; }; if($@) { print $@; # I expect this to print "foo", it does $logger->debug('test'); print $@; # I expect this to print "foo", it doesn't! print 'after printing $@'; } print "\n";
yourlogger.conf:
log4perl.rootLogger = DEBUG, stdout, file log4perl.appender.stdout = Log::Log4perl::Appender::Screen log4perl.appender.stdout.layout = Log::Log4perl::Layout::SimpleLayout log4perl.appender.stdout.stderr = 1 log4perl.appender.stdout.Threshold = DEBUG log4perl.appender.file = Log::Dispatch::FileRotate log4perl.appender.file.mode = append log4perl.appender.file.size = 10_000_000 log4perl.appender.file.max = 10 log4perl.appender.file.filename = foo.txt log4perl.appender.file.layout=PatternLayout log4perl.appender.file.layout.ConversionPattern=%H:%P (%c) %p - %d: {% +X{work}} %m%n
What am I doing wrong?