fixing minor meta-circularity issue
[gitmo/Class-MOP.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.02';
9
10 use base 'Class::MOP::Class';
11
12 __PACKAGE__->meta->add_attribute(
13     Class::MOP::Attribute->new('$:count' => (
14         reader  => 'get_count',
15         default => 0
16     ))
17 );
18
19 sub construct_instance {
20     my ($class, %params) = @_;
21     $class->{'$:count'}++;
22     return $class->SUPER::construct_instance();
23 }
24
25 1;
26
27 __END__
28
29 =pod
30
31 =head1 NAME 
32
33 InstanceCountingClass - An example metaclass which counts instances
34
35 =head1 SYNOPSIS
36
37   package Foo;
38   
39   sub meta { InstanceCountingClass->initialize($_[0]) }
40   sub new  {
41       my $class = shift;
42       bless $class->meta->construct_instance(@_) => $class;
43   }
44
45   # ... meanwhile, somewhere in the code
46
47   my $foo = Foo->new();
48   print Foo->meta->get_count(); # prints 1
49   
50   my $foo2 = Foo->new();
51   print Foo->meta->get_count(); # prints 2  
52   
53   # ... etc etc etc
54
55 =head1 DESCRIPTION
56
57 This is a classic example of a metaclass which keeps a count of each 
58 instance which is created. 
59
60 =head1 AUTHOR
61
62 Stevan Little E<lt>stevan@iinteractive.comE<gt>
63
64 =head1 COPYRIGHT AND LICENSE
65
66 Copyright 2006 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