foo
[gitmo/Class-MOP.git] / lib / Class / MOP / Package.pm
CommitLineData
2243a22b 1
2package Class::MOP::Package;
3
4use strict;
5use warnings;
6
7use Scalar::Util 'blessed';
6d5355c3 8use Carp 'confess';
2243a22b 9
10our $VERSION = '0.01';
11
12# introspection
13
14sub meta {
15 require Class::MOP::Class;
16 Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
17}
18
6d5355c3 19# creation ...
20
21sub initialize {
22 my ($class, $package) = @_;
23 bless { '$:package' => $package } => $class;
24}
25
26# Attributes
27
28# NOTE:
29# all these attribute readers will be bootstrapped
30# away in the Class::MOP bootstrap section
31
32sub name { $_[0]->{'$:package'} }
33
34# Class attributes
35
36my %SIGIL_MAP = (
37 '$' => 'SCALAR',
38 '@' => 'ARRAY',
39 '%' => 'HASH',
40 '&' => 'CODE',
41);
42
43sub add_package_variable {
44 my ($self, $variable, $initial_value) = @_;
45
46 (defined $variable)
47 || confess "You must pass a variable name";
48
49 my ($sigil, $name) = ($variable =~ /^(.)(.*)$/);
50
51 (defined $sigil)
52 || confess "The variable name must include a sigil";
53
54 (exists $SIGIL_MAP{$sigil})
55 || confess "I do not recognize that sigil '$sigil'";
56
57 no strict 'refs';
58 no warnings 'misc';
59 *{$self->name . '::' . $name} = $initial_value;
60}
61
62sub has_package_variable {
63 my ($self, $variable) = @_;
64 (defined $variable)
65 || confess "You must pass a variable name";
66
67 my ($sigil, $name) = ($variable =~ /^(.)(.*)$/);
68
69 (defined $sigil)
70 || confess "The variable name must include a sigil";
71
72 (exists $SIGIL_MAP{$sigil})
73 || confess "I do not recognize that sigil '$sigil'";
74
75 no strict 'refs';
76 defined *{$self->name . '::' . $name}{$SIGIL_MAP{$sigil}} ? 1 : 0;
77
78}
79
80sub get_package_variable {
81 my ($self, $variable) = @_;
82 (defined $variable)
83 || confess "You must pass a variable name";
84
85 my ($sigil, $name) = ($variable =~ /^(.)(.*)$/);
86
87 (defined $sigil)
88 || confess "The variable name must include a sigil";
89
90 (exists $SIGIL_MAP{$sigil})
91 || confess "I do not recognize that sigil '$sigil'";
92
93 no strict 'refs';
94 return *{$self->name . '::' . $name}{$SIGIL_MAP{$sigil}};
95
96}
97
98sub remove_package_variable {
99 my ($self, $variable) = @_;
100
101 (defined $variable)
102 || confess "You must pass a variable name";
103
104 my ($sigil, $name) = ($variable =~ /^(.)(.*)$/);
105
106 (defined $sigil)
107 || confess "The variable name must include a sigil";
108
109 (exists $SIGIL_MAP{$sigil})
110 || confess "I do not recognize that sigil '$sigil'";
111
7f436b8c 112 no strict 'refs';
113 if ($SIGIL_MAP{$sigil} eq 'SCALAR') {
114 undef ${$self->name . '::' . $name};
115 }
116 elsif ($SIGIL_MAP{$sigil} eq 'ARRAY') {
117 undef @{$self->name . '::' . $name};
118 }
119 elsif ($SIGIL_MAP{$sigil} eq 'HASH') {
120 undef %{$self->name . '::' . $name};
121 }
122 elsif ($SIGIL_MAP{$sigil} eq 'CODE') {
123 undef &{$self->name . '::' . $name};
124 }
125 else {
126 confess "This should never ever ever happen";
127 }
6d5355c3 128}
129
130
2243a22b 1311;
132
133__END__
134
135=pod
136
137=head1 NAME
138
139Class::MOP::Package - Package Meta Object
140
141=head1 SYNOPSIS
142
143=head1 DESCRIPTION
144
145=head1 METHODS
146
147=over 4
148
149=item B<meta>
150
6d5355c3 151=item B<initialize>
152
153=item B<name>
154
155=item B<add_package_variable>
156
157=item B<get_package_variable>
158
159=item B<has_package_variable>
160
161=item B<remove_package_variable>
162
2243a22b 163=back
164
165=head1 AUTHOR
166
167Stevan Little E<lt>stevan@iinteractive.comE<gt>
168
169=head1 COPYRIGHT AND LICENSE
170
171Copyright 2006 by Infinity Interactive, Inc.
172
173L<http://www.iinteractive.com>
174
175This library is free software; you can redistribute it and/or modify
176it under the same terms as Perl itself.
177
178=cut