ChangeLog and adding yuval to authors everywhere
[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
a4258ffd 8our $VERSION = '0.03';
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
a4258ffd 17InstanceCountingClass->meta->add_before_method_modifier('construct_instance' => sub {
18 my ($class) = @_;
19 $class->{'$:count'}++;
20});
1a7ebbb3 21
e2f8b029 221;
23
24__END__
25
26=pod
27
28=head1 NAME
29
30InstanceCountingClass - An example metaclass which counts instances
31
32=head1 SYNOPSIS
33
34 package Foo;
35
677eb158 36 use metaclass 'InstanceCountingClass';
37
e2f8b029 38 sub new {
39 my $class = shift;
5659d76e 40 $class->meta->new_object(@_);
e2f8b029 41 }
42
43 # ... meanwhile, somewhere in the code
44
45 my $foo = Foo->new();
46 print Foo->meta->get_count(); # prints 1
47
48 my $foo2 = Foo->new();
49 print Foo->meta->get_count(); # prints 2
50
51 # ... etc etc etc
52
53=head1 DESCRIPTION
54
55This is a classic example of a metaclass which keeps a count of each
56instance which is created.
57
1a09d9cc 58=head1 AUTHORS
e2f8b029 59
60Stevan Little E<lt>stevan@iinteractive.comE<gt>
61
1a09d9cc 62Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
63
e2f8b029 64=head1 COPYRIGHT AND LICENSE
65
66Copyright 2006 by Infinity Interactive, Inc.
67
68L<http://www.iinteractive.com>
69
70This library is free software; you can redistribute it and/or modify
71it under the same terms as Perl itself.
72
73=cut