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

Re: [5.8.12] Proposal for changing UNIVERSAL semantics

Thread Previous | Thread Next
From:
Tels
Date:
November 4, 2003 06:28
Subject:
Re: [5.8.12] Proposal for changing UNIVERSAL semantics
Message ID:
200311041527.35997@bloodgate.com
-----BEGIN PGP SIGNED MESSAGE-----

Moin,

[youvegotpost.de? Denglish Alert!]

An einem sonnigen Herbstdienstag, Michael schrieb:

> On 2003-11-04 10:49:14 Dave Mitchell <davem@fdgroup.com> was overheard 
>saying:
>> 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 bench.pl
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 bench.pl
#!/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,

Tels

- -- 
 Signed on Tue Nov  4 15:23:05 2003 with key 0x93B84C15.
 Visit my photo gallery at http://bloodgate.com/photos/
 PGP key on http://bloodgate.com/tels.asc 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." --
 http://tinyurl.com/qmg5

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

iQEVAwUBP6e3VncLPEOTuEwVAQE6Uwf/Q+ZaCoHfKdvhmQTq9nZSVYXwCrEypupE
qoFR6ttvAjE27oFpDJdfvgEwx7OZEtBhxrYLBdaSQbl+UvU+1fd0Xk+9nors8QUq
YXPUlvd2ztajnQkLS5RcKKHi5nvuAf1z/o9VuzoUdvCPp6JrMVui0/YVmkufau3m
eS5PBk7nMr8kxRDUL3HK/Zpxtip4l/Z/erLQY8Vix+Bcbxxkt+s7W1yh/nhcC1km
KLB732sCDBpkQfjc7oxRUfxCR3XUuvIAxnAcVS+NezWIZRlpHkmO+alpDjSajmSf
dOkAh1K1Ly65tj8slG1/p+s5v9ViFiZ4cZPwZUkD1OMlCP8NLFh8hg==
=2K9w
-----END PGP SIGNATURE-----


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About