|laziness, impatience, and hubris|
Don't rely on someone else's module. The area is too complicated.Interesting. This is exactly why I think a module is needed. It is too complicated to be reimplemented and at least my first version was flawed or incomplete. I'd be more inclined to trust a module if the documentation and test suite give the impression of the module being robust.
This means you have to craft your own solution, but it is fairly simple.That sounds a bit contradictory. :-)
Define a DESTROY method as bad stuff happens otherwise.How about return if $method eq 'DESTROY'; in AUTOLOAD?
Define a semantically correct "can" function that either returns undef or a CODE ref.Would that include looking at UNIVERSAL::can to find "real" methods?
Define the AUTOLOAD function in the fairly standard way based upon the "can" function.Someone might decide to overload can and use caller arbitrarily, which may break (since there is no uplevel function in Perl). One might do this to remove some methods in can from the public (they can of course still be called). Good idea or not; I'm not to judge. I think caller was the reason I moved the logic out from can and into a common routine shared between can and AUTOLOAD. That way if anyone overloads can they do not have to think about my AUTOLOAD implementation since AUTOLOAD does not call can and in can I can use goto &$next to make it fully transparent.
I avoid multiple inheritence so I would make autoloading in a multiple inheritence scenario an absolute no.It's not harder to use next::method/next::can and mro::get_linear_isa/Algorithm::C3::merge than it is to use SUPER::foo/can('SUPER::foo') and Class::ISA::super_path (mro/Algorithm::C3/Class::ISA is only needed (?) when overloading AUTOLOAD). mro.pm is core since Perl 5.9.5.
If you wanted to allow subroutine stubs in your classes (including any subclasses) you'd need to take some special care, I believe. This may be to break your first rule above, but still. The person who subclasses your class might not agree so if it can be solved I favor solving it. The problem is that UNIVERSAL::can returns a reference, but the subroutine it references is undefined, so AUTOLOAD will be invoked. So to avoid fatal recursion can may not return a reference to an undefined subroutine (i.e. stub). I think you may run into trouble with $self->SUPER::foo and/or $self->can('SUPER::foo') as well. ($self->SUPER::foo invokes AUTOLOAD only if it's a stub.)
The reciprocal perspective is that you want to overload a method and use SUPER in a subclass to a class (that perhaps someone else wrote) that uses stubs to lazily provide methods. If AUTOLOAD would query can then you'd end up with a fatal recursion.
Coincidentally, the design of Class::AUTOCAN implicitly handles stubs seamlessly. :-) If you want to lazily provide a method but still want to give it the precedence of a real method then you just do
Maybe I should upload Class::AUTOCAN somewhere so that you can try it out and see if you find any bugs in it. I would appreciate any fire your or anyone else could put it under. :-) If it's not bullet proof then it's not worth having on CPAN.
Update: improved the example.
In reply to Re^6: A working strategy to handle AUTOLOAD, can(), and (multiple) inheritance without touching UNIVERSAL?