Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I keep a set of objects in Perl?

I would like to keep a set of (Moose) objects, so each object could only appear once.

I thought of using a hash where the key is the address of the object then check for existence of the key before I add an object. Is that a common practice? How do I get the address of the object?

UPDATE

On second thought, what's wrong with simply using the object reference as the key:

my %objects = ();

# some object (just created or or taken from somewhere...)
my $object ...

# add object to set
$objects{$object} = $object;

# operate on all objects
foreach my $obj (values %objects) {
...
}
like image 571
David B Avatar asked Dec 10 '22 12:12

David B


1 Answers

You can use Set::Object.

To get the address of a reference, you can use Scalar::Util::refaddr.

like image 125
Sinan Ünür Avatar answered Feb 04 '23 03:02

Sinan Ünür