don't allow invalid package names
[gitmo/Package-Stash.git] / lib / Package / Stash / PP.pm
CommitLineData
2905fb35 1package Package::Stash::PP;
f10f6217 2use strict;
3use warnings;
2905fb35 4# ABSTRACT: pure perl implementation of the Package::Stash API
f10f6217 5
7ef54f40 6use B;
f10f6217 7use Carp qw(confess);
eb53b1bd 8use Scalar::Util qw(blessed reftype weaken);
dc378b60 9use Symbol;
10# before 5.12, assigning to the ISA glob would make it lose its magical ->isa
11# powers
12use constant BROKEN_ISA_ASSIGNMENT => ($] < 5.012);
36cbfbad 13# before 5.10, stashes don't ever seem to drop to a refcount of zero, so
14# weakening them isn't helpful
15use constant BROKEN_WEAK_STASH => ($] < 5.010);
7ef54f40 16# before 5.10, the scalar slot was always treated as existing if the
17# glob existed
18use constant BROKEN_SCALAR_INITIALIZATION => ($] < 5.010);
f4979588 19
f4979588 20=head1 SYNOPSIS
21
2905fb35 22 use Package::Stash;
f4979588 23
24=head1 DESCRIPTION
25
2905fb35 26This is a backend for L<Package::Stash> implemented in pure perl, for those without a compiler or who would like to use this inline in scripts.
f4979588 27
28=cut
29
f10f6217 30sub new {
31 my $class = shift;
e55970bc 32 my ($package) = @_;
774f4f10 33
34 if (!defined($package) || (ref($package) && ref($package) ne 'HASH')) {
35 confess "Package::Stash->new must be passed the name of the "
36 . "package to access";
37 }
38 elsif (ref($package) eq 'HASH') {
39 confess "The pure perl implementation of Package::Stash doesn't "
40 . "currently support anonymous stashes. You should install "
41 . "Package::Stash::XS";
42 }
ed131e41 43 elsif ($package !~ /[0-9A-Z_a-z]+(?:::[0-9A-Z_a-z]+)*/) {
44 confess "$package is not a module name";
45 }
774f4f10 46
e55970bc 47 return bless {
eb53b1bd 48 'package' => $package,
e55970bc 49 }, $class;
f10f6217 50}
51
52sub name {
2905fb35 53 confess "Can't call name as a class method"
54 unless blessed($_[0]);
f10f6217 55 return $_[0]->{package};
56}
57
58sub namespace {
2905fb35 59 confess "Can't call namespace as a class method"
60 unless blessed($_[0]);
eb53b1bd 61
36cbfbad 62 if (BROKEN_WEAK_STASH) {
eb53b1bd 63 no strict 'refs';
36cbfbad 64 return \%{$_[0]->name . '::'};
eb53b1bd 65 }
36cbfbad 66 else {
67 return $_[0]->{namespace} if defined $_[0]->{namespace};
eb53b1bd 68
36cbfbad 69 {
70 no strict 'refs';
71 $_[0]->{namespace} = \%{$_[0]->name . '::'};
72 }
73
74 weaken($_[0]->{namespace});
eb53b1bd 75
36cbfbad 76 return $_[0]->{namespace};
77 }
f10f6217 78}
79
80{
81 my %SIGIL_MAP = (
82 '$' => 'SCALAR',
83 '@' => 'ARRAY',
84 '%' => 'HASH',
85 '&' => 'CODE',
56a29840 86 '' => 'IO',
f10f6217 87 );
88
89 sub _deconstruct_variable_name {
90 my ($self, $variable) = @_;
91
56a29840 92 (defined $variable && length $variable)
f10f6217 93 || confess "You must pass a variable name";
94
95 my $sigil = substr($variable, 0, 1, '');
96
56a29840 97 if (exists $SIGIL_MAP{$sigil}) {
98 return ($variable, $sigil, $SIGIL_MAP{$sigil});
99 }
100 else {
101 return ("${sigil}${variable}", '', $SIGIL_MAP{''});
102 }
f10f6217 103 }
104}
105
3634ce60 106sub _valid_for_type {
107 my $self = shift;
108 my ($value, $type) = @_;
109 if ($type eq 'HASH' || $type eq 'ARRAY'
110 || $type eq 'IO' || $type eq 'CODE') {
111 return reftype($value) eq $type;
112 }
113 else {
114 my $ref = reftype($value);
d173d3a3 115 return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE' || $ref eq 'REGEXP' || $ref eq 'VSTRING';
3634ce60 116 }
117}
118
2905fb35 119sub add_symbol {
640de369 120 my ($self, $variable, $initial_value, %opts) = @_;
f10f6217 121
122 my ($name, $sigil, $type) = ref $variable eq 'HASH'
123 ? @{$variable}{qw[name sigil type]}
124 : $self->_deconstruct_variable_name($variable);
125
4ada57e0 126 my $pkg = $self->name;
127
3634ce60 128 if (@_ > 2) {
129 $self->_valid_for_type($initial_value, $type)
130 || confess "$initial_value is not of type $type";
3634ce60 131
4ada57e0 132 # cheap fail-fast check for PERLDBf_SUBLINE and '&'
133 if ($^P and $^P & 0x10 && $sigil eq '&') {
640de369 134 my $filename = $opts{filename};
135 my $first_line_num = $opts{first_line_num};
4ada57e0 136
640de369 137 (undef, $filename, $first_line_num) = caller
4ada57e0 138 if not defined $filename;
640de369 139
140 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
4ada57e0 141
142 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
640de369 143 $DB::sub{$pkg . '::' . $name} = "$filename:$first_line_num-$last_line_num";
4ada57e0 144 }
145 }
f10f6217 146
147 no strict 'refs';
148 no warnings 'redefine', 'misc', 'prototype';
149 *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
150}
151
2905fb35 152sub remove_glob {
f10f6217 153 my ($self, $name) = @_;
65b8253b 154 delete $self->namespace->{$name};
f10f6217 155}
156
2905fb35 157sub has_symbol {
f10f6217 158 my ($self, $variable) = @_;
159
160 my ($name, $sigil, $type) = ref $variable eq 'HASH'
161 ? @{$variable}{qw[name sigil type]}
162 : $self->_deconstruct_variable_name($variable);
163
164 my $namespace = $self->namespace;
165
166 return unless exists $namespace->{$name};
167
168 my $entry_ref = \$namespace->{$name};
169 if (reftype($entry_ref) eq 'GLOB') {
f7543739 170 if ($type eq 'SCALAR') {
7ef54f40 171 if (BROKEN_SCALAR_INITIALIZATION) {
172 return defined ${ *{$entry_ref}{$type} };
173 }
174 else {
175 return B::svref_2object($entry_ref)->SV->isa('B::SV');
176 }
f7543739 177 }
178 else {
179 return defined *{$entry_ref}{$type};
180 }
f10f6217 181 }
182 else {
183 # a symbol table entry can be -1 (stub), string (stub with prototype),
184 # or reference (constant)
185 return $type eq 'CODE';
186 }
187}
188
2905fb35 189sub get_symbol {
e55803fc 190 my ($self, $variable, %opts) = @_;
f10f6217 191
192 my ($name, $sigil, $type) = ref $variable eq 'HASH'
193 ? @{$variable}{qw[name sigil type]}
194 : $self->_deconstruct_variable_name($variable);
195
196 my $namespace = $self->namespace;
197
7486ccf3 198 if (!exists $namespace->{$name}) {
dc378b60 199 if ($opts{vivify}) {
200 if ($type eq 'ARRAY') {
201 if (BROKEN_ISA_ASSIGNMENT) {
2905fb35 202 $self->add_symbol(
dc378b60 203 $variable,
204 $name eq 'ISA' ? () : ([])
205 );
206 }
207 else {
2905fb35 208 $self->add_symbol($variable, []);
dc378b60 209 }
210 }
211 elsif ($type eq 'HASH') {
2905fb35 212 $self->add_symbol($variable, {});
dc378b60 213 }
214 elsif ($type eq 'SCALAR') {
2905fb35 215 $self->add_symbol($variable);
dc378b60 216 }
217 elsif ($type eq 'IO') {
2905fb35 218 $self->add_symbol($variable, Symbol::geniosym);
dc378b60 219 }
220 elsif ($type eq 'CODE') {
221 confess "Don't know how to vivify CODE variables";
222 }
223 else {
224 confess "Unknown type $type in vivication";
225 }
5d3589c8 226 }
e55803fc 227 else {
4723417a 228 return undef;
e55803fc 229 }
30d1a098 230 }
f10f6217 231
232 my $entry_ref = \$namespace->{$name};
233
234 if (ref($entry_ref) eq 'GLOB') {
235 return *{$entry_ref}{$type};
236 }
237 else {
238 if ($type eq 'CODE') {
239 no strict 'refs';
240 return \&{ $self->name . '::' . $name };
241 }
242 else {
243 return undef;
244 }
245 }
246}
247
2905fb35 248sub get_or_add_symbol {
e55803fc 249 my $self = shift;
2905fb35 250 $self->get_symbol(@_, vivify => 1);
e55803fc 251}
252
2905fb35 253sub remove_symbol {
f10f6217 254 my ($self, $variable) = @_;
255
256 my ($name, $sigil, $type) = ref $variable eq 'HASH'
257 ? @{$variable}{qw[name sigil type]}
258 : $self->_deconstruct_variable_name($variable);
259
260 # FIXME:
9a3d1390 261 # no doubt this is grossly inefficient and
f10f6217 262 # could be done much easier and faster in XS
263
b1a00d0e 264 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
f10f6217 265 { sigil => '$', type => 'SCALAR', name => $name },
266 { sigil => '@', type => 'ARRAY', name => $name },
267 { sigil => '%', type => 'HASH', name => $name },
268 { sigil => '&', type => 'CODE', name => $name },
b1a00d0e 269 { sigil => '', type => 'IO', name => $name },
f10f6217 270 );
271
b1a00d0e 272 my ($scalar, $array, $hash, $code, $io);
f10f6217 273 if ($type eq 'SCALAR') {
2905fb35 274 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
275 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
276 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
277 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
f10f6217 278 }
279 elsif ($type eq 'ARRAY') {
7ef54f40 280 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
2905fb35 281 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
282 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
283 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
f10f6217 284 }
285 elsif ($type eq 'HASH') {
7ef54f40 286 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
2905fb35 287 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
288 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
289 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
f10f6217 290 }
291 elsif ($type eq 'CODE') {
7ef54f40 292 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
2905fb35 293 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
294 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
295 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
b1a00d0e 296 }
297 elsif ($type eq 'IO') {
7ef54f40 298 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
2905fb35 299 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
300 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
301 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
f10f6217 302 }
303 else {
304 confess "This should never ever ever happen";
305 }
306
2905fb35 307 $self->remove_glob($name);
f10f6217 308
7ef54f40 309 $self->add_symbol($scalar_desc => $scalar) if defined $scalar;
2905fb35 310 $self->add_symbol($array_desc => $array) if defined $array;
311 $self->add_symbol($hash_desc => $hash) if defined $hash;
312 $self->add_symbol($code_desc => $code) if defined $code;
313 $self->add_symbol($io_desc => $io) if defined $io;
f10f6217 314}
315
2905fb35 316sub list_all_symbols {
f10f6217 317 my ($self, $type_filter) = @_;
318
319 my $namespace = $self->namespace;
320 return keys %{$namespace} unless defined $type_filter;
321
322 # NOTE:
9a3d1390 323 # or we can filter based on
f10f6217 324 # type (SCALAR|ARRAY|HASH|CODE)
325 if ($type_filter eq 'CODE') {
326 return grep {
25c87f5c 327 # any non-typeglob in the symbol table is a constant or stub
328 ref(\$namespace->{$_}) ne 'GLOB'
329 # regular subs are stored in the CODE slot of the typeglob
d1f721b3 330 || defined(*{$namespace->{$_}}{CODE})
331 } keys %{$namespace};
332 }
333 elsif ($type_filter eq 'SCALAR') {
334 return grep {
7ef54f40 335 BROKEN_SCALAR_INITIALIZATION
336 ? (ref(\$namespace->{$_}) eq 'GLOB'
337 && defined(${*{$namespace->{$_}}{'SCALAR'}}))
338 : (do {
339 my $entry = \$namespace->{$_};
340 ref($entry) eq 'GLOB'
341 && B::svref_2object($entry)->SV->isa('B::SV')
342 })
d1f721b3 343 } keys %{$namespace};
344 }
345 else {
346 return grep {
347 ref(\$namespace->{$_}) eq 'GLOB'
348 && defined(*{$namespace->{$_}}{$type_filter})
f10f6217 349 } keys %{$namespace};
f10f6217 350 }
351}
f4979588 352
2905fb35 353sub get_all_symbols {
354 my ($self, $type_filter) = @_;
355
356 my $namespace = $self->namespace;
357 return { %{$namespace} } unless defined $type_filter;
358
359 return {
360 map { $_ => $self->get_symbol({name => $_, type => $type_filter}) }
361 $self->list_all_symbols($type_filter)
362 }
363}
364
0992f4ec 365=head1 BUGS
366
2905fb35 367=over 4
368
2905fb35 369=item * remove_symbol also replaces the associated typeglob
370
371This can cause unexpected behavior when doing manipulation at compile time -
372removing subroutines will still allow them to be called from within the package
373as subroutines (although they will not be available as methods). This can be
374considered a feature in some cases (this is how L<namespace::clean> works, for
375instance), but should not be relied upon - use C<remove_glob> directly if you
376want this behavior.
377
cdb543b5 378=item * Some minor memory leaks
379
380The pure perl implementation has a couple minor memory leaks (see the TODO
381tests in t/20-leaks.t) that I'm having a hard time tracking down - these may be
382core perl bugs, it's hard to tell.
383
2905fb35 384=back
0992f4ec 385
386Please report any bugs through RT: email
387C<bug-package-stash at rt.cpan.org>, or browse to
388L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
389
f4979588 390=head1 SEE ALSO
391
f4979588 392=over 4
393
988beb41 394=item * L<Class::MOP::Package>
f4979588 395
988beb41 396This module is a factoring out of code that used to live here
f4979588 397
398=back
399
0992f4ec 400=head1 SUPPORT
401
402You can find this documentation for this module with the perldoc command.
403
404 perldoc Package::Stash
405
406You can also look for information at:
407
408=over 4
409
410=item * AnnoCPAN: Annotated CPAN documentation
411
412L<http://annocpan.org/dist/Package-Stash>
413
414=item * CPAN Ratings
415
416L<http://cpanratings.perl.org/d/Package-Stash>
417
418=item * RT: CPAN's request tracker
419
420L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
421
422=item * Search CPAN
423
424L<http://search.cpan.org/dist/Package-Stash>
425
426=back
427
428=head1 AUTHOR
429
430Jesse Luehrs <doy at tozt dot net>
431
432Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
433Moose Cabal.
434
a912fc4b 435=begin Pod::Coverage
436
437BROKEN_ISA_ASSIGNMENT
438add_symbol
439get_all_symbols
440get_or_add_symbol
441get_symbol
442has_symbol
443list_all_symbols
444name
445namespace
446new
447remove_glob
448
449=end Pod::Coverage
450
f4979588 451=cut
452
4531;