2 package # hide the package from PAUSE
10 use base 'Class::MOP::Class';
12 InstanceCountingClass->meta->add_attribute('$:count' => (
13 reader => 'get_count',
17 sub construct_instance {
18 my ($class, %params) = @_;
19 $class->{'$:count'}++;
20 return $class->SUPER::construct_instance(%params);
31 InstanceCountingClass - An example metaclass which counts instances
37 use metaclass 'InstanceCountingClass';
41 $class->meta->new_object(@_);
44 # ... meanwhile, somewhere in the code
47 print Foo->meta->get_count(); # prints 1
49 my $foo2 = Foo->new();
50 print Foo->meta->get_count(); # prints 2
56 This is a classic example of a metaclass which keeps a count of each
57 instance which is created.
61 Stevan Little E<lt>stevan@iinteractive.comE<gt>
63 =head1 COPYRIGHT AND LICENSE
65 Copyright 2006 by Infinity Interactive, Inc.
67 L<http://www.iinteractive.com>
69 This library is free software; you can redistribute it and/or modify
70 it under the same terms as Perl itself.