develooper Front page | perl.perl5.porters | Postings from November 2003

Re: [5.8.12] Proposal for changing UNIVERSAL semantics

Thread Previous | Thread Next
November 4, 2003 06:28
Re: [5.8.12] Proposal for changing UNIVERSAL semantics
Message ID:


[ Denglish Alert!]

An einem sonnigen Herbstdienstag, Michael schrieb:

> On 2003-11-04 10:49:14 Dave Mitchell <> was overheard 
>> On Mon, Nov 03, 2003 at 12:37:49PM -0800, Michael Jacob wrote:
>>> I think, this change should not break any well written module. Testing
>>> for any method that's in UNIVERSAL is just useless, we already know
>>> before the test that it must be there. Any module that want's to know if
>>> there is some additional method in UNIVERSAL:: can just test against
>>> UNIVERSAL->can() directly.
>>But C<can> doesn't just test for existance, it returns a code ref of the
>>method if found. Thus C<$code = $obj->can('can')> is perfectly
>>reasonable, and should return a ref to UNIVERSAL::can if there aren't
>>any others in the inheritance chain.
>Show me one CPAN module that calls can() on anything (other than UNIVERSAL)
> to get a coderef for can(), isa() or VERSION().

I didn't know that it returns a code ref. BigInt has code like:

	if $CALC->can('_add') { ... do something }

Now, caching the lookup might give us a tad more speed, but as show below 
caching the test makes it twice as fast, while caching the actual code-ref 
doesn't makes it only a further 20% faster...

So, I could live with can() only returning true.

te@null:~/perl/spams> perl
Benchmark: running cache, can, has for at least 5 CPU seconds...
     cache:  6 s (5.39 usr +  0.01 sys =  5.40 CPU) @ 1543282/s (n=8333726)
       can:  5 s (5.29 usr +  0.00 sys =  5.29 CPU) @  632335/s (n=3345056)
       has:  4 s (5.05 usr +  0.00 sys =  5.05 CPU) @ 1281006/s (n=6469083)
te@null:~/perl/spams> cat
#!/usr/bin/perl -w
package Test;
sub test { return $_[0]; }
package main;
use Benchmark;
my $CALC = 'Test';
my $has; $has->{test} = $CALC->can('test');
timethese ( -5,
  can => sub { $CALC->test(0) if $CALC->can('test'); },
  has => sub { $CALC->test(0) if $has->{test}; },
  cache => sub { &{$has->{test}}(0) if $has; },
  } );

Best wishes,


- -- 
 Signed on Tue Nov  4 15:23:05 2003 with key 0x93B84C15.
 Visit my photo gallery at
 PGP key on or per email.

 "We have problems like this all of the time," Kirk said, trying to
 reassure me.  "Sometimes its really hard to get things burning." --

Version: GnuPG v1.2.2-rc1-SuSE (GNU/Linux)
Comment: When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About