| 1 |
NAME |
| 2 |
Object::ID - A unique identifier for any object |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
package My::Object; |
| 6 |
|
| 7 |
# Imports the object_id method |
| 8 |
use Object::ID; |
| 9 |
|
| 10 |
DESCRIPTION |
| 11 |
This is a unique identifier for any object, regardless of its type, |
| 12 |
structure or contents. Its features are: |
| 13 |
|
| 14 |
* Works on ANY object of any type |
| 15 |
* Does not modify the object in any way |
| 16 |
* Does not change with the object's contents |
| 17 |
* Is O(1) to calculate (ie. doesn't matter how big the object is) |
| 18 |
* The id is unique for the life of the process |
| 19 |
* The id is always a true value |
| 20 |
|
| 21 |
USAGE |
| 22 |
Object::ID is a role, rather than inheriting its methods they are |
| 23 |
imported into your class. To make your class use Object::ID, simply "use |
| 24 |
Object::ID" in your class. |
| 25 |
|
| 26 |
package My::Class; |
| 27 |
|
| 28 |
use Object::ID; |
| 29 |
|
| 30 |
Then write your class however you want. |
| 31 |
|
| 32 |
METHODS |
| 33 |
The following methods are made available to your class. |
| 34 |
|
| 35 |
object_id |
| 36 |
my $id = $object->object_id; |
| 37 |
|
| 38 |
Returns an identifier unique to the $object. |
| 39 |
|
| 40 |
The identifier is not related to the content of the object. It is only |
| 41 |
unique for the life of the process. There is no guarantee as to the |
| 42 |
format of the identifier from version to version. |
| 43 |
|
| 44 |
For example: |
| 45 |
|
| 46 |
my $obj = My::Class->new; |
| 47 |
my $copy = $obj; |
| 48 |
|
| 49 |
# This is true, $obj and $copy refer to the same object |
| 50 |
$obj->object_id eq $copy->object_id; |
| 51 |
|
| 52 |
my $obj2 = My::Class->new; |
| 53 |
|
| 54 |
# This is false, $obj and $obj2 are different objects. |
| 55 |
$obj->object_id eq $obj2->object_id; |
| 56 |
|
| 57 |
use Clone; |
| 58 |
my $clone = clone($obj); |
| 59 |
|
| 60 |
# This is false, even though they contain the same data. |
| 61 |
$obj->object_id eq $clone->object_id; |
| 62 |
|
| 63 |
object_uuid |
| 64 |
my $uuid = $object->object_uuid |
| 65 |
|
| 66 |
Like "$object->object_id" but returns a UUID unique to the $object. |
| 67 |
|
| 68 |
Only works if Data::UUID is installed. |
| 69 |
|
| 70 |
See Data::UUID for more details about UUID. |
| 71 |
|
| 72 |
FAQ |
| 73 |
Why not just use the object's reference? |
| 74 |
References are not unique over the life of a process. Perl will reuse |
| 75 |
references of destroyed objects, as demonstrated by this code snippet: |
| 76 |
|
| 77 |
{ |
| 78 |
package Foo; |
| 79 |
|
| 80 |
sub new { |
| 81 |
my $class = shift; |
| 82 |
my $string = shift; |
| 83 |
return bless {}, $class; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
for(1..3) { |
| 88 |
my $obj = Foo->new; |
| 89 |
print "Object's reference is $obj\n"; |
| 90 |
} |
| 91 |
|
| 92 |
This will print, for example, "Object's reference is Foo=HASH(0x803704)" |
| 93 |
three times. |
| 94 |
|
| 95 |
How much memory does it use? |
| 96 |
Very little. |
| 97 |
|
| 98 |
Object::ID stores the ID and address of each object you've asked the ID |
| 99 |
of. Once the object has been destroyed it no longer stores it. In other |
| 100 |
words, you only pay for what you use. When you're done with it, you |
| 101 |
don't pay for it any more. |
| 102 |
|
| 103 |
LICENSE |
| 104 |
Copyright 2010, Michael G Schwern <[email protected]>. |
| 105 |
|
| 106 |
This program is free software; you can redistribute it and/or modify it |
| 107 |
under the same terms as Perl itself. |
| 108 |
|
| 109 |
See <http://www.perl.com/perl/misc/Artistic.html> |
| 110 |
|
| 111 |
THANKS |
| 112 |
Thank you to Vincent Pit for coming up with the implementation. |
| 113 |
|