Not possible in the general case to the best of my knowledge. The only in-built object identifier is the address - so AFAIK there is no way of avoiding the (unlikely) situation of both objects getting the same address without adding some form of unique ID to the class.

It will also fail if either object overloads +, etc. to return something that is not the address - use refaddr if this is a concern.

If the reason you want this is to test that an object is being DESTROYed because it falls out of scope you can always use weaken like this.

I suppose you could do something totally evil, like overriding bless to store a UID somewhere for each object blessed. However this seems excessive :-)

Can you give us an idea of why you need to do this? I can't think of a situation where it would be necessary (although this is probably just a lack of imagination on my part).

Of course, if this is only an issue for one class it would be simplest to just add a UID slot to each object.


In reply to Re: Object identity? by adrianh
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.