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