Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Best way to compare two objects?

by jeffa (Bishop)
on Mar 22, 2016 at 19:16 UTC ( #1158533=note: print w/replies, xml ) Need Help??

in reply to Best way to compare two objects?

"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. :)


(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: Best way to compare two objects?
by nysus (Parson) on Mar 22, 2016 at 19:29 UTC

    It is the latter. I am pulling event information from a web page and then checking the event information periodically for updates. The event objects get stored in a file which then gets compared periodically with the latest information.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      How are you storing your objects in a file? Sounds like it might be better to store the data as JSON or in some other file-friendly format, read it back and compare Perl data structures. For that, there are plenty of tools.

      Also consider storing the response Last-Modified or Content-Length data and using a HEAD request on your check so you don't have to fetch the whole page each time to see if it's changed.

      The way forward always starts with a minimal test.

        I'm using storable to store the objects. I was data structures to store the data in the files but it was too messy. I've found moving to an OO format greatly simplifies the code.

        The website I'm getting the information from does not have a Last-Modified header. Content-Length doesn't tell me if the time of an event changed from "8 pm" to "9 pm".

        $PM = "Perl Monk's";
        $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
        $nysus = $PM . ' ' . $MCF;
        Click here if you love Perl Monks

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1158533]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2022-01-22 16:51 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (63 votes). Check out past polls.