fixing version numbers and writing the changelog
[gitmo/Class-MOP.git] / lib / metaclass.pm
CommitLineData
677eb158 1
2package metaclass;
3
4use strict;
5use warnings;
6
22286063 7use Carp 'confess';
8use Scalar::Util 'blessed';
677eb158 9
1becdfcc 10our $VERSION = '0.03';
677eb158 11
12use Class::MOP;
13
14sub import {
15 shift;
1becdfcc 16 my $metaclass;
17 if (!defined($_[0]) || $_[0] =~ /^\:(attribute|method|instance)_metaclass/) {
18 $metaclass = 'Class::MOP::Class';
19 }
20 else {
21 $metaclass = shift;
22 ($metaclass->isa('Class::MOP::Class'))
16e960bd 23 || confess "The metaclass ($metaclass) must be derived from Class::MOP::Class";
1becdfcc 24 }
d82060fe 25 my %options = @_;
26 my $package = caller();
677eb158 27
677eb158 28 # create a meta object so we can install &meta
29 my $meta = $metaclass->initialize($package => %options);
30 $meta->add_method('meta' => sub {
31 # we must re-initialize so that it
32 # works as expected in subclasses,
33 # since metaclass instances are
34 # singletons, this is not really a
35 # big deal anyway.
22286063 36 $metaclass->initialize((blessed($_[0]) || $_[0]) => %options)
677eb158 37 });
38}
39
401;
41
42__END__
43
44=pod
45
46=head1 NAME
47
96ceced8 48metaclass - a pragma for installing and using Class::MOP metaclasses
677eb158 49
50=head1 SYNOPSIS
51
550d56db 52 package MyClass;
53
54 # use Class::MOP::Class
55 use metaclass;
56
57 # ... or use a custom metaclass
677eb158 58 use metaclass 'MyMetaClass';
59
550d56db 60 # ... or use a custom metaclass
61 # and custom attribute and method
62 # metaclasses
677eb158 63 use metaclass 'MyMetaClass' => (
64 ':attribute_metaclass' => 'MyAttributeMetaClass',
65 ':method_metaclass' => 'MyMethodMetaClass',
66 );
67
1becdfcc 68 # ... or just specify custom attribute
69 # and method classes, and Class::MOP::Class
70 # is the assumed metaclass
71 use metaclass (
72 ':attribute_metaclass' => 'MyAttributeMetaClass',
73 ':method_metaclass' => 'MyMethodMetaClass',
74 );
75
677eb158 76=head1 DESCRIPTION
77
c9e77dbb 78This is a pragma to make it easier to use a specific metaclass
550d56db 79and a set of custom attribute and method metaclasses. It also
80installs a C<meta> method to your class as well.
c9e77dbb 81
1a09d9cc 82=head1 AUTHORS
677eb158 83
84Stevan Little E<lt>stevan@iinteractive.comE<gt>
85
1a09d9cc 86Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
87
677eb158 88=head1 COPYRIGHT AND LICENSE
89
90Copyright 2006 by Infinity Interactive, Inc.
91
92L<http://www.iinteractive.com>
93
94This library is free software; you can redistribute it and/or modify
95it under the same terms as Perl itself.
96
16e960bd 97=cut