Skip Alien-Ditaa
[gitmo/Moose.git] / examples / InstanceCountingClass.pod
1
2 package # hide the package from PAUSE
3     InstanceCountingClass;
4
5 use strict;
6 use warnings;
7
8 our $VERSION = '0.03';
9
10 use base 'Class::MOP::Class';
11
12 InstanceCountingClass->meta->add_attribute('count' => (
13     reader  => 'get_count',
14     default => 0
15 ));
16
17 InstanceCountingClass->meta->add_before_method_modifier('_construct_instance' => sub {
18     my ($class) = @_;
19     $class->{'count'}++;        
20 });
21
22 1;
23
24 __END__
25
26 =pod
27
28 =head1 NAME 
29
30 InstanceCountingClass - 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
55 This is a classic example of a metaclass which keeps a count of each 
56 instance which is created. 
57
58 =head1 AUTHORS
59
60 Stevan Little E<lt>stevan@iinteractive.comE<gt>
61
62 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
63
64 =head1 COPYRIGHT AND LICENSE
65
66 Copyright 2006-2008 by Infinity Interactive, Inc.
67
68 L<http://www.iinteractive.com>
69
70 This library is free software; you can redistribute it and/or modify
71 it under the same terms as Perl itself. 
72
73 =cut