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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.