The following code is copied directly from Class::WeakSingleton's test.pl. I only care about this for testing so I know that I'm getting the same object when I expect to and not when I don't. I think I'll just put in an object counter and compare *that* since everything else would involve gyrations it seems.
Added: adrianh said I wasn't clear about what this code shows. If I had a way of comparing past object identity with present objects then I could assert $s1_addr != Class::WeakSingleton->instnace. It turns out that there's a much easier solution which just has me assigning a unique value to each object (just a $counter++) so I can determine identity in the case for my specific problem. The general case may not be solvable.
{ my %h; { # call Class::WeakSingleton->instance() twice and expect to ge +t the same # reference returned on both occasions. my $s1 = Class::WeakSingleton->instance(); my $s2 = Class::WeakSingleton->instance(); ok( $s1 == $s2 ); # Test 4 ok( $s1 == Class::WeakSingleton->instance ); ok( $Class::WeakSingleton::_instance == $s1 ); # Test 5 $h{test} = $s1; } ok( $Class::WeakSingleton::_instance ); # Test 6 } ok( not defined $Class::WeakSingleton::_instance ); # Test 7 { { # call MySingleton->instance() twice and expect to get the sam +e # reference returned on both occasions. my $s3 = DerivedSingleton->instance(); my $s4 = DerivedSingleton->instance(); $s5 = DerivedSingleton->instance; ok( $s3 == $s4 ); ok( $s4 == $s5 ); } ok( $s5 == DerivedSingleton->instance ); undef $s5; ok( not $DerivedSingleton::_instance ); }
In reply to Re: Re: Object identity?
by diotalevi
in thread Object identity?
by diotalevi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |