2 package InstanceCountingClass;
11 use base 'Class::MOP::Class';
13 __PACKAGE__->meta->add_attribute(
14 Class::MOP::Attribute->new('$:count' => (
15 reader => 'get_count',
20 sub construct_instance {
21 my ($class, %params) = @_;
22 $class->{'$:count'}++;
23 return $class->SUPER::construct_instance();
34 InstanceCountingClass - An example metaclass which counts instances
40 sub meta { InstanceCountingClass->initialize($_[0]) }
43 bless $class->meta->construct_instance() => $class;
46 # ... meanwhile, somewhere in the code
49 print Foo->meta->get_count(); # prints 1
51 my $foo2 = Foo->new();
52 print Foo->meta->get_count(); # prints 2
58 This is a classic example of a metaclass which keeps a count of each
59 instance which is created.
63 Stevan Little E<lt>stevan@iinteractive.comE<gt>
65 =head1 COPYRIGHT AND LICENSE
67 Copyright 2006 by Infinity Interactive, Inc.
69 L<http://www.iinteractive.com>
71 This library is free software; you can redistribute it and/or modify
72 it under the same terms as Perl itself.