tests, and make this actually work
[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 }
12b5662c 43 elsif ($package !~ /\A[0-9A-Z_a-z]+(?:::[0-9A-Z_a-z]+)*\z/) {
ed131e41 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
875d2d1d 95 # XXX in pure perl, this will access things in inner packages,
96 # in xs, this will segfault - probably look more into this at
97 # some point
98 ($variable !~ /::/)
99 || confess "Variable names may not contain ::";
100
f10f6217 101 my $sigil = substr($variable, 0, 1, '');
102
56a29840 103 if (exists $SIGIL_MAP{$sigil}) {
104 return ($variable, $sigil, $SIGIL_MAP{$sigil});
105 }
106 else {
107 return ("${sigil}${variable}", '', $SIGIL_MAP{''});
108 }
f10f6217 109 }
110}
111
3634ce60 112sub _valid_for_type {
113 my $self = shift;
114 my ($value, $type) = @_;
115 if ($type eq 'HASH' || $type eq 'ARRAY'
116 || $type eq 'IO' || $type eq 'CODE') {
117 return reftype($value) eq $type;
118 }
119 else {
120 my $ref = reftype($value);
d173d3a3 121 return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE' || $ref eq 'REGEXP' || $ref eq 'VSTRING';
3634ce60 122 }
123}
124
2905fb35 125sub add_symbol {
640de369 126 my ($self, $variable, $initial_value, %opts) = @_;
f10f6217 127
128 my ($name, $sigil, $type) = ref $variable eq 'HASH'
129 ? @{$variable}{qw[name sigil type]}
130 : $self->_deconstruct_variable_name($variable);
131
4ada57e0 132 my $pkg = $self->name;
133
3634ce60 134 if (@_ > 2) {
135 $self->_valid_for_type($initial_value, $type)
136 || confess "$initial_value is not of type $type";
3634ce60 137
4ada57e0 138 # cheap fail-fast check for PERLDBf_SUBLINE and '&'
139 if ($^P and $^P & 0x10 && $sigil eq '&') {
640de369 140 my $filename = $opts{filename};
141 my $first_line_num = $opts{first_line_num};
4ada57e0 142
640de369 143 (undef, $filename, $first_line_num) = caller
4ada57e0 144 if not defined $filename;
640de369 145
146 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
4ada57e0 147
148 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
640de369 149 $DB::sub{$pkg . '::' . $name} = "$filename:$first_line_num-$last_line_num";
4ada57e0 150 }
151 }
f10f6217 152
153 no strict 'refs';
154 no warnings 'redefine', 'misc', 'prototype';
155 *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
156}
157
2905fb35 158sub remove_glob {
f10f6217 159 my ($self, $name) = @_;
65b8253b 160 delete $self->namespace->{$name};
f10f6217 161}
162
2905fb35 163sub has_symbol {
f10f6217 164 my ($self, $variable) = @_;
165
166 my ($name, $sigil, $type) = ref $variable eq 'HASH'
167 ? @{$variable}{qw[name sigil type]}
168 : $self->_deconstruct_variable_name($variable);
169
170 my $namespace = $self->namespace;
171
172 return unless exists $namespace->{$name};
173
174 my $entry_ref = \$namespace->{$name};
175 if (reftype($entry_ref) eq 'GLOB') {
f7543739 176 if ($type eq 'SCALAR') {
7ef54f40 177 if (BROKEN_SCALAR_INITIALIZATION) {
178 return defined ${ *{$entry_ref}{$type} };
179 }
180 else {
181 return B::svref_2object($entry_ref)->SV->isa('B::SV');
182 }
f7543739 183 }
184 else {
185 return defined *{$entry_ref}{$type};
186 }
f10f6217 187 }
188 else {
189 # a symbol table entry can be -1 (stub), string (stub with prototype),
190 # or reference (constant)
191 return $type eq 'CODE';
192 }
193}
194
2905fb35 195sub get_symbol {
e55803fc 196 my ($self, $variable, %opts) = @_;
f10f6217 197
198 my ($name, $sigil, $type) = ref $variable eq 'HASH'
199 ? @{$variable}{qw[name sigil type]}
200 : $self->_deconstruct_variable_name($variable);
201
202 my $namespace = $self->namespace;
203
7486ccf3 204 if (!exists $namespace->{$name}) {
dc378b60 205 if ($opts{vivify}) {
206 if ($type eq 'ARRAY') {
207 if (BROKEN_ISA_ASSIGNMENT) {
2905fb35 208 $self->add_symbol(
dc378b60 209 $variable,
210 $name eq 'ISA' ? () : ([])
211 );
212 }
213 else {
2905fb35 214 $self->add_symbol($variable, []);
dc378b60 215 }
216 }
217 elsif ($type eq 'HASH') {
2905fb35 218 $self->add_symbol($variable, {});
dc378b60 219 }
220 elsif ($type eq 'SCALAR') {
2905fb35 221 $self->add_symbol($variable);
dc378b60 222 }
223 elsif ($type eq 'IO') {
2905fb35 224 $self->add_symbol($variable, Symbol::geniosym);
dc378b60 225 }
226 elsif ($type eq 'CODE') {
227 confess "Don't know how to vivify CODE variables";
228 }
229 else {
230 confess "Unknown type $type in vivication";
231 }
5d3589c8 232 }
e55803fc 233 else {
4723417a 234 return undef;
e55803fc 235 }
30d1a098 236 }
f10f6217 237
238 my $entry_ref = \$namespace->{$name};
239
240 if (ref($entry_ref) eq 'GLOB') {
241 return *{$entry_ref}{$type};
242 }
243 else {
244 if ($type eq 'CODE') {
245 no strict 'refs';
246 return \&{ $self->name . '::' . $name };
247 }
248 else {
249 return undef;
250 }
251 }
252}
253
2905fb35 254sub get_or_add_symbol {
e55803fc 255 my $self = shift;
2905fb35 256 $self->get_symbol(@_, vivify => 1);
e55803fc 257}
258
2905fb35 259sub remove_symbol {
f10f6217 260 my ($self, $variable) = @_;
261
262 my ($name, $sigil, $type) = ref $variable eq 'HASH'
263 ? @{$variable}{qw[name sigil type]}
264 : $self->_deconstruct_variable_name($variable);
265
266 # FIXME:
9a3d1390 267 # no doubt this is grossly inefficient and
f10f6217 268 # could be done much easier and faster in XS
269
b1a00d0e 270 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
f10f6217 271 { sigil => '$', type => 'SCALAR', name => $name },
272 { sigil => '@', type => 'ARRAY', name => $name },
273 { sigil => '%', type => 'HASH', name => $name },
274 { sigil => '&', type => 'CODE', name => $name },
b1a00d0e 275 { sigil => '', type => 'IO', name => $name },
f10f6217 276 );
277
b1a00d0e 278 my ($scalar, $array, $hash, $code, $io);
f10f6217 279 if ($type eq 'SCALAR') {
2905fb35 280 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
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 'ARRAY') {
7ef54f40 286 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
2905fb35 287 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_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 'HASH') {
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 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
295 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
f10f6217 296 }
297 elsif ($type eq 'CODE') {
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 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
b1a00d0e 302 }
303 elsif ($type eq 'IO') {
7ef54f40 304 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
2905fb35 305 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
306 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
307 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
f10f6217 308 }
309 else {
310 confess "This should never ever ever happen";
311 }
312
2905fb35 313 $self->remove_glob($name);
f10f6217 314
7ef54f40 315 $self->add_symbol($scalar_desc => $scalar) if defined $scalar;
2905fb35 316 $self->add_symbol($array_desc => $array) if defined $array;
317 $self->add_symbol($hash_desc => $hash) if defined $hash;
318 $self->add_symbol($code_desc => $code) if defined $code;
319 $self->add_symbol($io_desc => $io) if defined $io;
f10f6217 320}
321
2905fb35 322sub list_all_symbols {
f10f6217 323 my ($self, $type_filter) = @_;
324
325 my $namespace = $self->namespace;
326 return keys %{$namespace} unless defined $type_filter;
327
328 # NOTE:
9a3d1390 329 # or we can filter based on
f10f6217 330 # type (SCALAR|ARRAY|HASH|CODE)
331 if ($type_filter eq 'CODE') {
332 return grep {
25c87f5c 333 # any non-typeglob in the symbol table is a constant or stub
334 ref(\$namespace->{$_}) ne 'GLOB'
335 # regular subs are stored in the CODE slot of the typeglob
d1f721b3 336 || defined(*{$namespace->{$_}}{CODE})
337 } keys %{$namespace};
338 }
339 elsif ($type_filter eq 'SCALAR') {
340 return grep {
7ef54f40 341 BROKEN_SCALAR_INITIALIZATION
342 ? (ref(\$namespace->{$_}) eq 'GLOB'
343 && defined(${*{$namespace->{$_}}{'SCALAR'}}))
344 : (do {
345 my $entry = \$namespace->{$_};
346 ref($entry) eq 'GLOB'
347 && B::svref_2object($entry)->SV->isa('B::SV')
348 })
d1f721b3 349 } keys %{$namespace};
350 }
351 else {
352 return grep {
353 ref(\$namespace->{$_}) eq 'GLOB'
354 && defined(*{$namespace->{$_}}{$type_filter})
f10f6217 355 } keys %{$namespace};
f10f6217 356 }
357}
f4979588 358
2905fb35 359sub get_all_symbols {
360 my ($self, $type_filter) = @_;
361
362 my $namespace = $self->namespace;
363 return { %{$namespace} } unless defined $type_filter;
364
365 return {
366 map { $_ => $self->get_symbol({name => $_, type => $type_filter}) }
367 $self->list_all_symbols($type_filter)
368 }
369}
370
0992f4ec 371=head1 BUGS
372
2905fb35 373=over 4
374
2905fb35 375=item * remove_symbol also replaces the associated typeglob
376
377This can cause unexpected behavior when doing manipulation at compile time -
378removing subroutines will still allow them to be called from within the package
379as subroutines (although they will not be available as methods). This can be
380considered a feature in some cases (this is how L<namespace::clean> works, for
381instance), but should not be relied upon - use C<remove_glob> directly if you
382want this behavior.
383
cdb543b5 384=item * Some minor memory leaks
385
386The pure perl implementation has a couple minor memory leaks (see the TODO
387tests in t/20-leaks.t) that I'm having a hard time tracking down - these may be
388core perl bugs, it's hard to tell.
389
2905fb35 390=back
0992f4ec 391
392Please report any bugs through RT: email
393C<bug-package-stash at rt.cpan.org>, or browse to
394L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
395
f4979588 396=head1 SEE ALSO
397
f4979588 398=over 4
399
988beb41 400=item * L<Class::MOP::Package>
f4979588 401
988beb41 402This module is a factoring out of code that used to live here
f4979588 403
404=back
405
0992f4ec 406=head1 SUPPORT
407
408You can find this documentation for this module with the perldoc command.
409
410 perldoc Package::Stash
411
412You can also look for information at:
413
414=over 4
415
416=item * AnnoCPAN: Annotated CPAN documentation
417
418L<http://annocpan.org/dist/Package-Stash>
419
420=item * CPAN Ratings
421
422L<http://cpanratings.perl.org/d/Package-Stash>
423
424=item * RT: CPAN's request tracker
425
426L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
427
428=item * Search CPAN
429
430L<http://search.cpan.org/dist/Package-Stash>
431
432=back
433
434=head1 AUTHOR
435
436Jesse Luehrs <doy at tozt dot net>
437
438Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
439Moose Cabal.
440
a912fc4b 441=begin Pod::Coverage
442
443BROKEN_ISA_ASSIGNMENT
444add_symbol
445get_all_symbols
446get_or_add_symbol
447get_symbol
448has_symbol
449list_all_symbols
450name
451namespace
452new
453remove_glob
454
455=end Pod::Coverage
456
f4979588 457=cut
458
4591;