"I wish to compare two objects to determine if the attribute values are the same. "

Do you want to compare two whole objects or just one attribute value held by two objects? If it is the former, then this begs the question: why? The latter, however, is fairly easy to solve:

if ($object1->get_attr != $object2->get_attr) { print "they are different!" }
Since you you know which attribute you are comparing, you will know what its type is and how to compare it accordingly.

But back to the former case, comparing two whole objects. Rarely does one need to do this, which is probably why you found "very little guidance on this issue anywhere." It is not an issue for most of us. If you find yourself in a situation where you have to compare two whole objects then surely you can decompose the problem a little further. If you simply cannot, then most solutions for comparing whole objects involve writing a method that takes the object in question as an argument and inspects that object. This method (usually a class method) sees if it has every attribute required and if it can perform every method required. What is tricky is when the object in question has other attributes and methods. This is not a good road to take to find your solution. I recommend instead finding the 1 or 2 attributes or methods necessary to determine a delta. If you still insist on taking that road, look into overload the equality operator (=), which is a fairly natural thing to do in full featured OO languages, but not so much in Perl mostly because Perl has better ways to solve those problems. Good luck. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to Re: Best way to compare two objects? by jeffa
in thread Best way to compare two objects? by nysus

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.