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) {
...
}
You can use Set::Object.
To get the address of a reference, you can use Scalar::Util::refaddr.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With