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