[Chicago-talk] ->can and AUTOLOAD

Steven Lembark lembark at wrkhors.com
Thu Dec 4 10:58:31 CST 2003



-- Jay Strauss <me at heyjay.com>

> Asking.   But it doesn't matter, I'm just calling the method, and checking
> in my AUTOLOAD sub to see if this method is allowable

AUTOLOAD is used to fake out the symbol table by adding
things in on the fly that weren't there to find without
autoloading. can is used to query the symbol table.

Testing for $foo->( 'AUTOLOAD' ) is not reliable since
you have no guarantee of exactly what the autoloader
will be capable of implementing.

If you normally expect the methods to be available then
it's probably better to deal with them via exceptions
(a.k.a. block eval).

If $foo cannot $name and also cannot AUTOLOAD then you
know it's a failure. Combining the two you can get:

	if( my $sub = $foo->can($name) )
	{
		$foo->$sub( @argz );
	}
	elsif( $foo->can('AUTOLOAD') )
	{
		# doublecheck me on the AUTOLOAD syntax

		$foo->AUTOLOAD( $name, @argz );
	}
	else
	{
		my $type = ref $foo || $foo;

		die "Bogus call: '$foo' cannot '$name'";
	}

Catch: autoloading or the sub call may give you exceptions
also, so you have to either be REEEEEEL careful about what
you pick for the explicit die or not care why the call
failed (e.g., due to bogus args).


--
Steven Lembark                               2930 W. Palmer
Workhorse Computing                       Chicago, IL 60647
                                            +1 888 359 3508



More information about the Chicago-talk mailing list