getting close to a 0.07 release
[gitmo/Class-MOP.git] / examples / InstanceCountingClass.pod
CommitLineData
1a7ebbb3 1
9ec169fe 2package # hide the package from PAUSE
3 InstanceCountingClass;
1a7ebbb3 4
5use strict;
6use warnings;
7
99e5b7e8 8our $VERSION = '0.02';
1a7ebbb3 9
e2f8b029 10use base 'Class::MOP::Class';
1a7ebbb3 11
550d56db 12InstanceCountingClass->meta->add_attribute('$:count' => (
13 reader => 'get_count',
14 default => 0
15));
1a7ebbb3 16
17sub construct_instance {
18 my ($class, %params) = @_;
19 $class->{'$:count'}++;
550d56db 20 return $class->SUPER::construct_instance(%params);
1a7ebbb3 21}
22
e2f8b029 231;
24
25__END__
26
27=pod
28
29=head1 NAME
30
31InstanceCountingClass - An example metaclass which counts instances
32
33=head1 SYNOPSIS
34
35 package Foo;
36
677eb158 37 use metaclass 'InstanceCountingClass';
38
e2f8b029 39 sub new {
40 my $class = shift;
5659d76e 41 $class->meta->new_object(@_);
e2f8b029 42 }
43
44 # ... meanwhile, somewhere in the code
45
46 my $foo = Foo->new();
47 print Foo->meta->get_count(); # prints 1
48
49 my $foo2 = Foo->new();
50 print Foo->meta->get_count(); # prints 2
51
52 # ... etc etc etc
53
54=head1 DESCRIPTION
55
56This is a classic example of a metaclass which keeps a count of each
57instance which is created.
58
59=head1 AUTHOR
60
61Stevan Little E<lt>stevan@iinteractive.comE<gt>
62
63=head1 COPYRIGHT AND LICENSE
64
65Copyright 2006 by Infinity Interactive, Inc.
66
67L<http://www.iinteractive.com>
68
69This library is free software; you can redistribute it and/or modify
70it under the same terms as Perl itself.
71
72=cut