1 package Package::Stash::PP;
4 # ABSTRACT: pure perl implementation of the Package::Stash API
8 use Scalar::Util qw(blessed reftype weaken);
10 # before 5.12, assigning to the ISA glob would make it lose its magical ->isa
12 use constant BROKEN_ISA_ASSIGNMENT => ($] < 5.012);
13 # before 5.10, stashes don't ever seem to drop to a refcount of zero, so
14 # weakening them isn't helpful
15 use constant BROKEN_WEAK_STASH => ($] < 5.010);
16 # before 5.10, the scalar slot was always treated as existing if the
18 use constant BROKEN_SCALAR_INITIALIZATION => ($] < 5.010);
26 This 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.
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";
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";
45 'package' => $package,
50 confess "Can't call name as a class method"
51 unless blessed($_[0]);
52 return $_[0]->{package};
56 confess "Can't call namespace as a class method"
57 unless blessed($_[0]);
59 if (BROKEN_WEAK_STASH) {
61 return \%{$_[0]->name . '::'};
64 return $_[0]->{namespace} if defined $_[0]->{namespace};
68 $_[0]->{namespace} = \%{$_[0]->name . '::'};
71 weaken($_[0]->{namespace});
73 return $_[0]->{namespace};
86 sub _deconstruct_variable_name {
87 my ($self, $variable) = @_;
89 (defined $variable && length $variable)
90 || confess "You must pass a variable name";
92 my $sigil = substr($variable, 0, 1, '');
94 if (exists $SIGIL_MAP{$sigil}) {
95 return ($variable, $sigil, $SIGIL_MAP{$sigil});
98 return ("${sigil}${variable}", '', $SIGIL_MAP{''});
103 sub _valid_for_type {
105 my ($value, $type) = @_;
106 if ($type eq 'HASH' || $type eq 'ARRAY'
107 || $type eq 'IO' || $type eq 'CODE') {
108 return reftype($value) eq $type;
111 my $ref = reftype($value);
112 return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE' || $ref eq 'REGEXP' || $ref eq 'VSTRING';
117 my ($self, $variable, $initial_value, %opts) = @_;
119 my ($name, $sigil, $type) = ref $variable eq 'HASH'
120 ? @{$variable}{qw[name sigil type]}
121 : $self->_deconstruct_variable_name($variable);
123 my $pkg = $self->name;
126 $self->_valid_for_type($initial_value, $type)
127 || confess "$initial_value is not of type $type";
129 # cheap fail-fast check for PERLDBf_SUBLINE and '&'
130 if ($^P and $^P & 0x10 && $sigil eq '&') {
131 my $filename = $opts{filename};
132 my $first_line_num = $opts{first_line_num};
134 (undef, $filename, $first_line_num) = caller
135 if not defined $filename;
137 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
139 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
140 $DB::sub{$pkg . '::' . $name} = "$filename:$first_line_num-$last_line_num";
145 no warnings 'redefine', 'misc', 'prototype';
146 *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
150 my ($self, $name) = @_;
151 delete $self->namespace->{$name};
155 my ($self, $variable) = @_;
157 my ($name, $sigil, $type) = ref $variable eq 'HASH'
158 ? @{$variable}{qw[name sigil type]}
159 : $self->_deconstruct_variable_name($variable);
161 my $namespace = $self->namespace;
163 return unless exists $namespace->{$name};
165 my $entry_ref = \$namespace->{$name};
166 if (reftype($entry_ref) eq 'GLOB') {
167 if ($type eq 'SCALAR') {
168 if (BROKEN_SCALAR_INITIALIZATION) {
169 return defined ${ *{$entry_ref}{$type} };
172 return B::svref_2object($entry_ref)->SV->isa('B::SV');
176 return defined *{$entry_ref}{$type};
180 # a symbol table entry can be -1 (stub), string (stub with prototype),
181 # or reference (constant)
182 return $type eq 'CODE';
187 my ($self, $variable, %opts) = @_;
189 my ($name, $sigil, $type) = ref $variable eq 'HASH'
190 ? @{$variable}{qw[name sigil type]}
191 : $self->_deconstruct_variable_name($variable);
193 my $namespace = $self->namespace;
195 if (!exists $namespace->{$name}) {
197 if ($type eq 'ARRAY') {
198 if (BROKEN_ISA_ASSIGNMENT) {
201 $name eq 'ISA' ? () : ([])
205 $self->add_symbol($variable, []);
208 elsif ($type eq 'HASH') {
209 $self->add_symbol($variable, {});
211 elsif ($type eq 'SCALAR') {
212 $self->add_symbol($variable);
214 elsif ($type eq 'IO') {
215 $self->add_symbol($variable, Symbol::geniosym);
217 elsif ($type eq 'CODE') {
218 confess "Don't know how to vivify CODE variables";
221 confess "Unknown type $type in vivication";
229 my $entry_ref = \$namespace->{$name};
231 if (ref($entry_ref) eq 'GLOB') {
232 return *{$entry_ref}{$type};
235 if ($type eq 'CODE') {
237 return \&{ $self->name . '::' . $name };
245 sub get_or_add_symbol {
247 $self->get_symbol(@_, vivify => 1);
251 my ($self, $variable) = @_;
253 my ($name, $sigil, $type) = ref $variable eq 'HASH'
254 ? @{$variable}{qw[name sigil type]}
255 : $self->_deconstruct_variable_name($variable);
258 # no doubt this is grossly inefficient and
259 # could be done much easier and faster in XS
261 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
262 { sigil => '$', type => 'SCALAR', name => $name },
263 { sigil => '@', type => 'ARRAY', name => $name },
264 { sigil => '%', type => 'HASH', name => $name },
265 { sigil => '&', type => 'CODE', name => $name },
266 { sigil => '', type => 'IO', name => $name },
269 my ($scalar, $array, $hash, $code, $io);
270 if ($type eq 'SCALAR') {
271 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
272 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
273 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
274 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
276 elsif ($type eq 'ARRAY') {
277 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
278 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
279 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
280 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
282 elsif ($type eq 'HASH') {
283 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
284 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
285 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
286 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
288 elsif ($type eq 'CODE') {
289 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
290 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
291 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
292 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
294 elsif ($type eq 'IO') {
295 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
296 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
297 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
298 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
301 confess "This should never ever ever happen";
304 $self->remove_glob($name);
306 $self->add_symbol($scalar_desc => $scalar) if defined $scalar;
307 $self->add_symbol($array_desc => $array) if defined $array;
308 $self->add_symbol($hash_desc => $hash) if defined $hash;
309 $self->add_symbol($code_desc => $code) if defined $code;
310 $self->add_symbol($io_desc => $io) if defined $io;
313 sub list_all_symbols {
314 my ($self, $type_filter) = @_;
316 my $namespace = $self->namespace;
317 return keys %{$namespace} unless defined $type_filter;
320 # or we can filter based on
321 # type (SCALAR|ARRAY|HASH|CODE)
322 if ($type_filter eq 'CODE') {
324 # any non-typeglob in the symbol table is a constant or stub
325 ref(\$namespace->{$_}) ne 'GLOB'
326 # regular subs are stored in the CODE slot of the typeglob
327 || defined(*{$namespace->{$_}}{CODE})
328 } keys %{$namespace};
330 elsif ($type_filter eq 'SCALAR') {
332 BROKEN_SCALAR_INITIALIZATION
333 ? (ref(\$namespace->{$_}) eq 'GLOB'
334 && defined(${*{$namespace->{$_}}{'SCALAR'}}))
336 my $entry = \$namespace->{$_};
337 ref($entry) eq 'GLOB'
338 && B::svref_2object($entry)->SV->isa('B::SV')
340 } keys %{$namespace};
344 ref(\$namespace->{$_}) eq 'GLOB'
345 && defined(*{$namespace->{$_}}{$type_filter})
346 } keys %{$namespace};
350 sub get_all_symbols {
351 my ($self, $type_filter) = @_;
353 my $namespace = $self->namespace;
354 return { %{$namespace} } unless defined $type_filter;
357 map { $_ => $self->get_symbol({name => $_, type => $type_filter}) }
358 $self->list_all_symbols($type_filter)
366 =item * remove_symbol also replaces the associated typeglob
368 This can cause unexpected behavior when doing manipulation at compile time -
369 removing subroutines will still allow them to be called from within the package
370 as subroutines (although they will not be available as methods). This can be
371 considered a feature in some cases (this is how L<namespace::clean> works, for
372 instance), but should not be relied upon - use C<remove_glob> directly if you
375 =item * Some minor memory leaks
377 The pure perl implementation has a couple minor memory leaks (see the TODO
378 tests in t/20-leaks.t) that I'm having a hard time tracking down - these may be
379 core perl bugs, it's hard to tell.
383 Please report any bugs through RT: email
384 C<bug-package-stash at rt.cpan.org>, or browse to
385 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
391 =item * L<Class::MOP::Package>
393 This module is a factoring out of code that used to live here
399 You can find this documentation for this module with the perldoc command.
401 perldoc Package::Stash
403 You can also look for information at:
407 =item * AnnoCPAN: Annotated CPAN documentation
409 L<http://annocpan.org/dist/Package-Stash>
413 L<http://cpanratings.perl.org/d/Package-Stash>
415 =item * RT: CPAN's request tracker
417 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
421 L<http://search.cpan.org/dist/Package-Stash>
427 Jesse Luehrs <doy at tozt dot net>
429 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
434 BROKEN_ISA_ASSIGNMENT