unify the anon package stuff in CMOP::Package
[gitmo/Moose.git] / lib / Class / MOP / Module.pm
CommitLineData
38bf2a25 1
2package Class::MOP::Module;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8use Scalar::Util 'blessed';
9
38bf2a25 10use base 'Class::MOP::Package';
11
12sub _new {
13 my $class = shift;
14 return Class::MOP::Class->initialize($class)->new_object(@_)
15 if $class ne __PACKAGE__;
16
17 my $params = @_ == 1 ? $_[0] : {@_};
18 return bless {
9c1bf11e 19 # Need to quote package to avoid a problem with PPI mis-parsing this
20 # as a package statement.
38bf2a25 21
22 # from Class::MOP::Package
9c1bf11e 23 'package' => $params->{package},
38bf2a25 24 namespace => \undef,
25
26 # attributes
27 version => \undef,
28 authority => \undef
29 } => $class;
30}
31
32sub version {
33 my $self = shift;
34 ${$self->get_or_add_package_symbol('$VERSION')};
35}
36
37sub authority {
38 my $self = shift;
39 ${$self->get_or_add_package_symbol('$AUTHORITY')};
40}
41
42sub identifier {
43 my $self = shift;
44 join '-' => (
45 $self->name,
46 ($self->version || ()),
47 ($self->authority || ()),
48 );
49}
50
51sub create {
790ae571 52 my $class = shift;
53 my @args = @_;
54
55 unshift @args, 'package' if @args % 2 == 1;
56 my %options = @args;
57
58 my $package = delete $options{package};
59 my $version = delete $options{version};
60 my $authority = delete $options{authority};
61
62 my $meta = $class->SUPER::create($package => %options);
63
64 $meta->_instantiate_module($version, $authority);
65
66 return $meta;
38bf2a25 67}
68
790ae571 69sub _anon_package_prefix { 'Class::MOP::Module::__ANON__::SERIAL::' }
70sub _anon_cache_key { confess "Modules are not cacheable" }
71
72
38bf2a25 73sub _instantiate_module {
74 my($self, $version, $authority) = @_;
75 my $package_name = $self->name;
76
77 Class::MOP::_is_valid_class_name($package_name)
78 || confess "creation of $package_name failed: invalid package name";
79
80 no strict 'refs';
81 scalar %{ $package_name . '::' }; # touch the stash
82 ${ $package_name . '::VERSION' } = $version if defined $version;
83 ${ $package_name . '::AUTHORITY' } = $authority if defined $authority;
84
85 return;
86}
87
881;
89
90# ABSTRACT: Module Meta Object
91
92__END__
93
94=pod
95
96=head1 NAME
97
98Class::MOP::Module - Module Meta Object
99
100=head1 DESCRIPTION
101
102A module is essentially a L<Class::MOP::Package> with metadata, in our
103case the version and authority.
104
105=head1 INHERITANCE
106
107B<Class::MOP::Module> is a subclass of L<Class::MOP::Package>.
108
109=head1 METHODS
110
111=over 4
112
113=item B<< $metamodule->version >>
114
115This is a read-only attribute which returns the C<$VERSION> of the
116package, if one exists.
117
118=item B<< $metamodule->authority >>
119
120This is a read-only attribute which returns the C<$AUTHORITY> of the
121package, if one exists.
122
123=item B<< $metamodule->identifier >>
124
125This constructs a string which combines the name, version and
126authority.
127
128=item B<< Class::MOP::Module->meta >>
129
130This will return a L<Class::MOP::Class> instance for this class.
131
132=back
133
134=cut