From: Mike S. <log...@pe...> - 2003-06-04 04:37:56
|
On Tue, 3 Jun 2003, Gordon Marler wrote: > # tri-natured: function, class method, or object method > sub _classobj { > my $objclass = shift || __PACKAGE__; > my $class = ref($objclass) || $objclass; > no strict "refs"; # to convert sym ref to real one > return \%$class; > } > > for my $datum (keys %{ _classobj() } ) { > # turn off strict refs so that we can register a method > # in the symbol table > no strict "refs"; > *$datum = sub { > use strict "refs"; > my $self = shift->_classobj(); > $self->{$datum} = shift if @_; > return $self->{$datum}; > } > } Ok, the previously posted suggestions should fix the problem described here, but just out of curiosity: The reason for the warning message is very obscure. If you call the test program test.pl, consisting of use BE; $be = new BE; undef $be; print "\nfinished\n"; you'll get (in cleanup) Can't call method "log" on an undefined value at (eval 14) line 37 during global destruction with the BE.pm code previously posted by Gordon and attached at the end of this email. However, if you're using the debugger, and run it like perl -d test.pl DB<1> r you'll just get finished Debugged program terminated. Use q to quit or R to restart, Seems like the perl core gets confused with the reference count somehow. Would be interesting if we can isolate the problem to a couple of lines of code so we could submit it to p5p for further investigation ... -- Mike Mike Schilli log...@pe... http://perlmeister.com http://log4perl.sourceforge.net ############################################################ # Code for BE.pm ############################################################ package BE; use Log::Log4perl qw(:easy); %BE = ( logger => "", ConfigFile => "/etc/lb_lutab", # List of all BE objects ALL => [ ], ); #tri-natured: function, class method, or object method sub _classobj { my $objclass = shift || __PACKAGE__; my $class = ref($objclass) || $objclass; no strict "refs"; # to convert sym ref to real one return \%$class; } for my $datum (keys %{ _classobj() } ) { # turn off strict refs so that we can register a method # in the symbol table no strict "refs"; *$datum = sub { use strict "refs"; my $self = shift->_classobj(); $self->{$datum} = shift if @_; return $self->{$datum}; } } # Constructor sub new { my ($caller,%arg) = @_; my $caller_is_obj = ref($caller); my $class = $caller_is_obj || $caller; my ($logger); unless (BE->logger) { Log::Log4perl->easy_init($DEBUG); my $logger = Log::Log4perl->get_logger('BE'); print "logger is $logger\n"; BE->logger($logger); } $logger = Log::Log4perl->get_logger('BE'); my $instance = { _name => $arg{name} || # Name of this BE undef, #... Other members of the object ... _logger => undef, }; # Save a copy of BE->logger here so the reference to the logger can # for use inside DESTROY $instance->{_logger} = $logger; # Append this to the Class list of all BEs my ($all) = BE->ALL(); push @{$all}, $instance; BE->ALL($all); bless $instance, $class; } sub DESTROY { my ($self) = @_; my ($logger) = Log::Log4perl->get_logger("BE"); $logger->info("Entering DESTROY method for " . __PACKAGE__ . "\n"); } 1; |