Object::ID(3pm)
πΈ Object::ID π
okay bestie, so this is Object::ID, from the User Contributed Perl Documentation corner of the Object::ID(3pm) manual. it gives you a unique identifier for any object and it is genuinely iconic. let's go β¨
β¨ NAME β¨
Object::ID β a unique identifier for any object π
π¦ SYNOPSIS π¦
package My::Object;
# Imports the object_id method
use Object::ID;
π DESCRIPTION π
okay so listen. this is a unique identifier for any object, no matter what type it is, what its structure is, or what's inside it. truly any object, no cap. its features are:
- Works on ANY object of any type π
- Does not modify the object in any way (she would never, she's respectful)
- Does not change with the object's contents
- Is O(1) to calculate (ie. doesn't matter how big the object is) β constant time, she's not out here stressing π
- The id is unique for the life of the process
- The id is always a true value β
π USAGE π
Object::ID is a role, okay? it does not work by inheriting its methods. instead, they get imported into your class. so to make your class use Object::ID, you literally just use Object::ID in your class. that's it. periodt. π
package My::Class;
use Object::ID;
then write your class however you want, bestie. no rules, only vibes π¦
πΈ METHODS πΈ
the following methods are made available to your class:
π object_id
my $id = $object->object_id;
returns an identifier unique to the $object β¨
so like, the identifier is not related to the content of the object. it is only unique for the life of the process. and ngl there is no guarantee as to the format of the identifier from version to version β so don't get attached to the shape of it, bestie, she changes.
for example:
my $obj = My::Class->new;
my $copy = $obj;
# This is true, $obj and $copy refer to the same object
$obj->object_id eq $copy->object_id;
my $obj2 = My::Class->new;
# This is false, $obj and $obj2 are different objects.
$obj->object_id eq $obj2->object_id;
use Clone;
my $clone = clone($obj);
# This is false, even though they contain the same data.
$obj->object_id eq $clone->object_id;
π object_uuid
my $uuid = $object->object_uuid
like $object->object_id but it returns a UUID unique to the $object π¦
β οΈ she only works if Data::UUID is installed. no Data::UUID, no UUID. that's the deal.
see Data::UUID for more details about UUID π
π₯ FAQ π₯
π why not just use the object's reference?
because references are not unique over the life of a process, bestie. π Perl will reuse references of destroyed objects, as demonstrated by this code snippet:
{
package Foo;
sub new {
my $class = shift;
my $string = shift;
return bless {}, $class;
}
}
for(1..3) {
my $obj = Foo->new;
print "Object's reference is $obj\n";
}
this will print, for example, "Object's reference is Foo=HASH(0x803704)" three times. same reference, three different objects. not the vibe. π
π§ how much memory does it use?
very little β¨
Object::ID stores the ID and address of each object you've asked the ID of. and once the object has been destroyed, it no longer stores it. in other words: you only pay for what you use. when you're done with it, you don't pay for it any more. sustainable queen π
π LICENSE π
copyright 2010, Michael G Schwern <schwern@pobox.com> π
this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
see <http://www.perl.com/perl/misc/Artistic.html>
π«Ά THANKS π«Ά
thank you to Vincent Pit for coming up with the implementation. icon behavior, honestly. π
and that's the whole page, bestie!! π this was perl v5.36.0, dated 2022-06-16, straight from the Object::ID(3pm) manual. go forth and give everything an id, you main character π
β¨ππ¦
π¦ where this page comes from
- ubuntu noble: libobject-id-perl 0.1.2-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libobject-id-perl 0.1.2-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libobject-id-perl 0.1.2-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms