Last active
June 2, 2017 08:06
-
-
Save tobyink/9c2f47e02c18277e979238032fa47083 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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