AUTOLOAD (was: PHP sucks...)

Andy Armstrong andy at
Sat Jan 13 11:36:49 GMT 2007

On 13 Jan 2007, at 10:47, Tim Sweetman wrote:
> Hey, you should know better than that, you wrote the UNIVERSAL::can  
> module, to get around the problem of people saying
>   if (UNIVERSAL::can($object..) { ... }
> and getting the wrong answer (or, at least, not the answer they  
> wanted).
> (though as a longtime mod_perl developer, I get distinctly nervous  
> about modules which redefine things in the UNIVERSAL package).

I don't think that's what he's saying. He's saying you can do

package Foo;

sub can {
    # blah

    # similar blah

and then later

my $frib = Foo->new();

if ($frib->can('ponk')) {
     print "w00t!";

>> I presume anyone smart enough to write code in AUTOLOAD() which  
>> knows which methods to support is smart enough to figure out how  
>> to share that same logic with a custom can() method [...]
> Simple use of AUTOLOAD is trivial.
> Dealing with AUTOLOAD's effects on inheritance, the can method, and  
> other subtleties is enough to make experts go "hmm", consider, talk  
> a lot, write utility modules, and debate tickets (http:// 
> Damian's "Best Practices" say: don't use AUTOLOAD.
> I never use it, and recommend people against its use, because once  
> inheritance and cans are in the mix, the situation becomes  
> extremely unclear.

I believe AUTOMETHOD in Class::Std does the right thing.

Andy Armstrong,

More information about the mailing list