2 package # hide the package from PAUSE
10 use base 'Class::MOP::Class';
12 __PACKAGE__->meta->add_attribute(
13 Class::MOP::Attribute->new('$:count' => (
14 reader => 'get_count',
19 sub construct_instance {
20 my ($class, %params) = @_;
21 $class->{'$:count'}++;
22 return $class->SUPER::construct_instance();
33 InstanceCountingClass - An example metaclass which counts instances
39 sub meta { InstanceCountingClass->initialize($_[0]) }
42 bless $class->meta->construct_instance(@_) => $class;
45 # ... meanwhile, somewhere in the code
48 print Foo->meta->get_count(); # prints 1
50 my $foo2 = Foo->new();
51 print Foo->meta->get_count(); # prints 2
57 This is a classic example of a metaclass which keeps a count of each
58 instance which is created.
62 Stevan Little E<lt>stevan@iinteractive.comE<gt>
64 =head1 COPYRIGHT AND LICENSE
66 Copyright 2006 by Infinity Interactive, Inc.
68 L<http://www.iinteractive.com>
70 This library is free software; you can redistribute it and/or modify
71 it under the same terms as Perl itself.