Something Perl's OO has been missing has been a reliable way to identify an object. Is $this the same as $that? Not asking if it contains the same information, but is it a referent to the same object? Have we seen it before? When I alter $this will I also be changing $that?
package Foo;
use Object::ID;
...write the class however you want...
my $id = $obj->object_id; my $uuid = $obj->object_uuid;
object_id()
is a cheap, process-specific identifier. object_uuid()
is a bit more expensive on first call (it has to generate the UUID, about 30% slower) but it should be universally unique across machines and processes.package DateTime; use Object::ID;
my $date = DateTime->now; say $date->object_id;
use UNIVERSAL::Object::ID;
# Regexes are objects say qr/foo/->object_id;
# Loading IO::Handle turns all filehandles into objects use IO::Handle; open my $fh, "foo/bar"; say $fh->object_id;
{ package Foo;
sub new { my $class = shift; return bless {}, $class; } }
for(1..3) { my $obj = Foo->new; print "Object's reference is $obj\n"; }
use Hash::Util::FieldHash qw(fieldhash); fieldhash(my %IDs);
sub object_id { my $self = shift;
state $last_id = "a";
return $IDs{$self} //= ++$last_id; }
Perhaps you could add something like
Object::ID->object_id_for($object)
Or use Object::ID qw(obj_id);
These would both avoid UNIVERSAL and avoid needing to add *anything* to *any* class *ever*.
Every time someone puts something in UNIVERSAL, %%RND_BAD_THING%%.
Re:UNIVERSAL
schwern on 2010-05-02T19:55:48
All the objections associated with UNIVERSAL::isa($obj, $class) vs $obj->isa($class); come to mind. Why would you override object_id()? Its too early to say. Apparently we thought the same thing about isa() and can().
OTOH maybe someone might write their own object_id() method that does something different and you'll accidentally get that? Entirely possible, but turns out to be highly improbable. A Google Code Search there's only a handful of object_id() methods out there (I cut out BioPerl, Moco and Pogo because they artificially inflate the count).
Anyhow, this sort of feather ruffling is why UNIVERSAL::Object::ID is in its own package.
FWIW you can already call object_id($obj).
Re:UNIVERSAL
rjbs on 2010-05-02T21:44:25
"FWIW you can already call object_id($obj)."
Oh, of course, because although it is advertised that you're importing it to be called via your package as a method, it can be imported in for use as a function. Hooray!
Re:UNIVERSAL
Aristotle on 2010-05-02T23:20:37
You can check in
object_id_for
if the object provides a method can trampoline to that. Then you get to have it both ways.Re:UNIVERSAL
schwern on 2010-05-02T23:58:08
True, true.
Now that we've all had our OH GOD UNIVERSAL time, what do you think of the actual module?
Hash::FieldHash
Ron Savage on 2010-05-03T00:35:33
Hi
Why use the heavy-weight Hash::Util::FieldHash when the light-weight Hash::FieldHash is available?
Re:Hash::FieldHash
schwern on 2010-05-03T18:36:44
You're right, it is significantly faster. Over 2x faster. I'll do some more testing and switch it over.