InstanceCountingClass - An example metaclass which counts instances
package Foo; use metaclass 'InstanceCountingClass'; sub new { my $class = shift; $class->meta->new_object(@_); } # ... meanwhile, somewhere in the code my $foo = Foo->new(); print Foo->meta->get_count(); # prints 1 my $foo2 = Foo->new(); print Foo->meta->get_count(); # prints 2 # ... etc etc etc
This is a classic example of a metaclass which keeps a count of each instance which is created.
Stevan Little <stevan@iinteractive.com>
Yuval Kogman <nothingmuch@woobling.com>
Copyright 2006-2008 by Infinity Interactive, Inc.
http://www.iinteractive.com
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Moose, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Moose
CPAN shell
perl -MCPAN -e shell install Moose
For more information on module installation, please visit the detailed CPAN module installation guide.