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