AUTOLOAD (was: PHP sucks...)

Andy Armstrong andy at hexten.net
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
}

sub AUTOLOAD {
    # 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:// 
> rt.cpan.org/Public/Dist/Display.html?Name=UNIVERSAL-can)
>
> 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, hexten.net



More information about the london.pm mailing list