A mutable class will throw errors from inside CMOP, and that's expected
[gitmo/Moose.git] / examples / InstanceCountingClass.pod
CommitLineData
38bf2a25 1
2package # hide the package from PAUSE
3 InstanceCountingClass;
4
5use strict;
6use warnings;
7
8our $VERSION = '0.03';
9
10use base 'Class::MOP::Class';
11
12InstanceCountingClass->meta->add_attribute('count' => (
13 reader => 'get_count',
14 default => 0
15));
16
17InstanceCountingClass->meta->add_before_method_modifier('_construct_instance' => sub {
18 my ($class) = @_;
19 $class->{'count'}++;
20});
21
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
36 use metaclass 'InstanceCountingClass';
37
38 sub new {
39 my $class = shift;
40 $class->meta->new_object(@_);
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
58=head1 AUTHORS
59
60Stevan Little E<lt>stevan@iinteractive.comE<gt>
61
62Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
63
64=head1 COPYRIGHT AND LICENSE
65
66Copyright 2006-2008 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