in reply to Re^2: How to test caching?
in thread How to test caching?

What happens when you try it?

package Tie::Scalar::Yell; sub TIESCALAR { bless \my $x, shift } sub STORE { ${ $_[0] } = $_[1] } sub FETCH { warn "accessed\n"; return ${ $_[0] }; } package main; tie my $x, 'Tie::Scalar::Yell'; $x = 'yeller'; print "yeller: $x\n"; # accessed here copier( $x ); sub copier { my $arg = shift; # accessed here print "my arg once: $arg\n"; # ...or here print "my arg twice: $arg\n"; # here? } __END__ accessed yeller: yeller accessed my arg once: yeller my arg twice: yeller

The magic goes away. I didn't know that for sure when I posted, but I suspected that would be the case. That's why I also mentioned using overload.

package Overload::Yell; use overload '""' => sub { warn "string\n"; return 'foo' }, '0+' => sub { warn "number\n"; return 123 }, ; sub new { bless {} } package main; my $x = Overload::Yell->new(); print "yeller: $x\n"; # accessed here copier( $x ); sub copier { my $arg = shift; # accessed here print "my arg once: $arg\n"; # ...or here print "my arg twice: $arg\n"; # here? } __END__ string yeller: foo string my arg once: foo string my arg twice: foo

That works fine except for having to overload '=' too.

Either way, I think it's altogether too much hassle to test a cache so simple.

Replies are listed 'Best First'.
Re^4: How to test caching?
by repellent (Priest) on Aug 27, 2008 at 05:35 UTC
    Agreed. Thanks, kyle.