[Chicago-talk] ->can and AUTOLOAD

Steven Lembark lembark at wrkhors.com
Thu Dec 4 11:05:02 CST 2003

You can add a universal autoloader that delivers a more
descriptive error message then croaks. This guarantees
that $foo->$name will always be callable and that you
will get useful error messages if $name is meaningless
for the object/package:

		# discombobulate @_ and print a useful
		# description of why the sub does not
		# exist for this object/class/package.


		confess "Bogus call: $package cannot $name";

Now you can always use:

	my $sub = $foo->can( $name ) || $foo->can( 'AUTOLOAD' );

	$foo->$sub( @argz );

either you'll hit $name the fisrt time, hit a 'real' autoloader,
or bounce into UNIVERSAL::AUTOLOAD which can tell you exactly
where the process went wrong.

If you check the pod for the 5.8 version of Carp (may have
been there in 5.6) it has setups for classes to trust (i.e.,
report the error from however many levels of caller up the
stack it takes to get out of that class). Adding UNIVERSAL
and the dispatcher's package to this will show you where
the call with $name in it came from with less garbage to
trace through.

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

More information about the Chicago-talk mailing list