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