[Israel.pm] bless and AUTOLOAD

Yuval Kogman nothingmuch at woobling.org
Wed Sep 26 03:00:33 PDT 2007


On Wed, Sep 26, 2007 at 06:20:04 +0200, Yona Shlomo wrote:

> I agree with your statement that AUTOLOAD is best avoided.
> However, its functionality helps me a lot with not writing
> several dozens of similar functions that differ mostly by
> name in one place and not worry about them and their
> maintenance.

AUTOLOAD is often not the solution here, but rather something like
Yuval (Yaari) and Gaal proposed for reasons of predictability:

	if ( $object->can("method") ) {
		$object->method("method");
	}

this code might be confusing to debug if "method" can be called and
even if you override 'can' many people still use
UNIVERSAL::can($object,"method") (see the UNIVERSAL::can module)
breaking things anyway.

This is also impossible to introspect with e.g. Class::MOP or
Class::Inspector, etc.

Personally I feel the only valid use for AUTOLOAD is when an object
is masquerading as any number of other objects, for example a proxy
object that relays methods over a network, or a wrapper that can
handle all the methods of the object it wraps (and that object can
be different every time).

-- 
  Yuval Kogman <nothingmuch at woobling.org>
http://nothingmuch.woobling.org  0xEBD27418




More information about the Perl mailing list