Thread

Posted on Wed Dec 14 01:13:38 2005 by sbelton
Undefined subroutine &MyClass::new

Hello,

After finding out that my classes using Class::Std did not work with mod_perl I was delighted to find out that Object::InsideOut solved my problem.

However I have come across a different problem which did not occur when using Class::Std: I get an error when loading classes which use the Singleton pattern:

package MyClass;{ use Object::InsideOut; my $instance; sub _init :INIT{ my ($self, $arg_ref) = @_; $instance = $self; } sub instance(){ if (defined $instance){ return $instance; } else { return new(@_); # error occurs on this line } } }

The error is 'Undefined subroutine &MyClass::new' which seems to imply that the 'new' subroutine is not yet defined at the time that my 'instance' subroutine gets defined. The error does occurr in a consistent manner which makes it even harder to troubleshoot, if I change the order in which my objects are instanciated it can 'solve' the problem but obviously I need to find the root cause the erradicate it completely. I have tried using the CPAN module Class::Singleton instead of my own implementation but I get the same error. I do not get the error when running the app outside of the mod_perl environment but again that could just be a coincidence as the error is not occurring consistently...

Any ideas? Is it OK in theory to call 'MyClass->new' from within MyClass?

Many thanks
Steph
Direct Responses: 1481 | Write a response
Posted on Wed Dec 14 15:10:03 2005 by jdhedden in response to 1474
Re: Undefined subroutine &MyClass::new
The problem is that ->new() is a method that is exported by Object::InsideOut, and in the case of runtime loading, is something not available when the parser carries on to your instance() subroutine.

As ->new() is a method, the error can be avoided by invoking it that way:

sub instance { if (defined $instance) { return $instance; } else { my $class = shift; # Extract class from args return $class->new(@_); # Invoke ->new() method } }
Direct Responses: 1482 | Write a response
Posted on Wed Dec 14 15:30:42 2005 by sbelton in response to 1481
Re: Undefined subroutine &MyClass::new
That works great, many thanks
Write a response