various tweaks, and refactor _instantiate_module not to use eval STRING
[gitmo/Class-MOP.git] / lib / Class / MOP / Module.pm
1
2 package Class::MOP::Module;
3
4 use strict;
5 use warnings;
6
7 use Carp         'confess';
8 use Scalar::Util 'blessed';
9
10 our $VERSION   = '0.89';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
13
14 use base 'Class::MOP::Package';
15
16 sub _new{
17     my $class = shift;
18     return Class::MOP::Class->initialize($class)->new_object(@_)
19       if $class ne __PACKAGE__;
20
21     my $params = @_ == 1 ? $_[0] : {@_};
22     return bless {
23         # from Class::MOP::Package
24         package   => $params->{package},
25         namespace => \undef,
26
27         # attributes
28         version   => \undef,
29         authority => \undef
30     } => $class;
31 }
32
33 sub version {  
34     my $self = shift;
35     ${$self->get_package_symbol({ sigil => '$', type => 'SCALAR', name => 'VERSION' })};
36 }
37
38 sub authority {  
39     my $self = shift;
40     ${$self->get_package_symbol({ sigil => '$', type => 'SCALAR', name => 'AUTHORITY' })};
41 }
42
43 sub identifier {
44     my $self = shift;
45     join '-' => (
46         $self->name,
47         ($self->version   || ()),
48         ($self->authority || ()),
49     );
50 }
51
52 sub create {
53     confess "The Class::MOP::Module->create method has been made a private object method.\n";
54 }
55
56 sub _instantiate_module {
57     my($self, $version, $authority) = @_;
58     my $package_name = $self->name;
59
60     Class::MOP::_is_valid_class_name($package_name)
61         || confess "creation of $package_name failed: invalid package name";
62
63     no strict 'refs';
64     scalar %{$package_name . '::'}; # touch the stash
65     ${$package_name . '::VERSION'}   = $version   if defined $version;
66     ${$package_name . '::AUTHORITY'} = $authority if defined $authority;
67
68     return;
69 }
70
71 1;
72
73 __END__
74
75 =pod
76
77 =head1 NAME 
78
79 Class::MOP::Module - Module Meta Object
80
81 =head1 DESCRIPTION
82
83 A module is essentially a L<Class::MOP::Package> with metadata, in our
84 case the version and authority.
85
86 =head1 INHERITANCE
87
88 B<Class::MOP::Module> is a subclass of L<Class::MOP::Package>.
89
90 =head1 METHODS
91
92 =over 4
93
94 =item B<< $metamodule->version >>
95
96 This is a read-only attribute which returns the C<$VERSION> of the
97 package, if one exists.
98
99 =item B<< $metamodule->authority >>
100
101 This is a read-only attribute which returns the C<$AUTHORITY> of the
102 package, if one exists.
103
104 =item B<< $metamodule->identifier >>
105
106 This constructs a string which combines the name, version and
107 authority.
108
109 =item B<< Class::MOP::Module->meta >>
110
111 This will return a L<Class::MOP::Class> instance for this class.
112
113 =back
114
115 =head1 AUTHORS
116
117 Stevan Little E<lt>stevan@iinteractive.comE<gt>
118
119 =head1 COPYRIGHT AND LICENSE
120
121 Copyright 2006-2009 by Infinity Interactive, Inc.
122
123 L<http://www.iinteractive.com>
124
125 This library is free software; you can redistribute it and/or modify
126 it under the same terms as Perl itself.
127
128 =cut