Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tobyink
Last active June 2, 2017 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobyink/9c2f47e02c18277e979238032fa47083 to your computer and use it in GitHub Desktop.
Save tobyink/9c2f47e02c18277e979238032fa47083 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use Data::Dumper;
{
package My::Example;
use Moose;
use Scalar::Util qw(weaken);
use Hash::Util::FieldHash::Compat qw(fieldhash);
has name => (is => 'ro');
{
fieldhash my %instances;
sub BUILD {
my $self = shift;
weaken( $instances{$self} = $self );
}
sub instances {
# might want to think about sorting them
# in a non-random order
values %instances;
}
}
}
my $alice = My::Example->new(name => 'Alice');
my $bob = My::Example->new(name => 'Bob');
my $carol = My::Example->new(name => 'Carol');
print Dumper [ My::Example->instances ];
undef $bob;
print Dumper [ My::Example->instances ];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment