If you compare them using
eq or
==, all
you're doing is comparing their values as references. And
since they're two different objects--ie. not references
to the same object--they're two different references. So
they won't be equal.
To do a proper object comparison, you'll need to write a
method that compares two of your objects. One possibility
would be to serialize the objects and compare the serialized
strings:
my $obj1 = new Obj;
my $obj2 = new Obj;
## The two objects above are now equal, let's say.
## Serialize them, then compare the serialized
## data.
use Storable qw/freeze/;
if (freeze($obj1) eq freeze($obj2)) {
## They're the same.
}
In fact, you could even use
overload to overload
the comparison operator so that the proper comparison
happens automagically:
package Obj;
use Storable qw/freeze/;
use overload '==' => \&compare;
sub compare { return freeze($_[0]) eq freeze($_[1]) }
Now you can just say:
if ($obj1 == $obj2) {
# They're the same.
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.