in reply to Python 'is' command
I am assuming that you can do exactly the same in Perl using a ref command in conjunction with '=='.
Not quite, since ref returns the type of the reference, not its address. You'll need Scalar::Util's refaddr for the latter. By default, Perl references used in numeric context will return the memory address (perlref), however, if the Perl objects use operator overloading, this is no longer true - see my example here.
If you look at the Python docs, you'll see that if the class defines a __eq__ method, it is used for == comparisons, and the fallback is to check if it's the same object, i.e. it's basically the same behavior as Perl's operator overloading. Python's is is basically what BillKSmith implemented here (and it's the only "entirely correct" answer in this thread).
Note that the general concept of "does one object equal another" is actually a lot more difficult than it sounds. When are two database handles equal - when they connect to the same database, or they have the same state, and so on? When are two objects loaded through an ORM like DBIx::Class equal - when their primary key is equal, or when all of their fields are equal, and so on? In such cases it's easier to code a custom comparator instead of the class attempting to provide a sensible default.
|
|---|