8 use Scalar::Util 'blessed';
11 our $VERSION = '1.08';
12 $VERSION = eval $VERSION;
13 our $AUTHORITY = 'cpan:STEVAN';
18 my ( $class, @args ) = @_;
20 unshift @args, "metaclass" if @args % 2 == 1;
23 my $metaclass = delete $options{metaclass};
25 unless ( defined $metaclass ) {
26 $metaclass = "Class::MOP::Class";
28 Class::MOP::load_class($metaclass);
31 ($metaclass->isa('Class::MOP::Class'))
32 || confess "The metaclass ($metaclass) must be derived from Class::MOP::Class";
34 # make sure the custom metaclasses get loaded
35 foreach my $key (grep { /_(?:meta)?class$/ } keys %options) {
36 unless ( ref( my $class = $options{$key} ) ) {
37 Class::MOP::load_class($class)
41 my $package = caller();
43 # create a meta object so we can install &meta
44 my $meta = $metaclass->initialize($package => %options);
45 my $should_install = !delete $options{no_meta};
46 $meta->add_method('meta' => sub {
47 # we must re-initialize so that it
48 # works as expected in subclasses,
49 # since metaclass instances are
50 # singletons, this is not really a
52 if (Class::MOP::DEBUG_NO_META()) {
54 if (my $meta = try { $self->SUPER::meta }) {
55 return $meta if $meta->isa('Class::MOP::Class');
57 confess "'meta' method called by MOP internals"
58 if caller =~ /Class::MOP|metaclass/;
60 $metaclass->initialize((blessed($_[0]) || $_[0]) => %options)
61 }) if $should_install;
72 metaclass - a pragma for installing and using Class::MOP metaclasses
78 # use Class::MOP::Class
81 # ... or use a custom metaclass
82 use metaclass 'MyMetaClass';
84 # ... or use a custom metaclass
85 # and custom attribute and method
87 use metaclass 'MyMetaClass' => (
88 'attribute_metaclass' => 'MyAttributeMetaClass',
89 'method_metaclass' => 'MyMethodMetaClass',
92 # ... or just specify custom attribute
93 # and method classes, and Class::MOP::Class
94 # is the assumed metaclass
96 'attribute_metaclass' => 'MyAttributeMetaClass',
97 'method_metaclass' => 'MyMethodMetaClass',
100 # if we'd rather not install a 'meta' method, we can do this
101 use metaclass no_meta => 1;
105 This is a pragma to make it easier to use a specific metaclass
106 and a set of custom attribute and method metaclasses. It also
107 installs a C<meta> method to your class as well, if the
108 C<no_meta> option is not specified.
112 Stevan Little E<lt>stevan@iinteractive.comE<gt>
114 =head1 COPYRIGHT AND LICENSE
116 Copyright 2006-2010 by Infinity Interactive, Inc.
118 L<http://www.iinteractive.com>
120 This library is free software; you can redistribute it and/or modify
121 it under the same terms as Perl itself.