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";
43 elsif ($package !~ /\A[0-9A-Z_a-z]+(?:::[0-9A-Z_a-z]+)*\z/) {
44 confess "$package is not a module name";
48 'package' => $package,
53 confess "Can't call name as a class method"
54 unless blessed($_[0]);
55 return $_[0]->{package};
59 confess "Can't call namespace as a class method"
60 unless blessed($_[0]);
62 if (BROKEN_WEAK_STASH) {
64 return \%{$_[0]->name . '::'};
67 return $_[0]->{namespace} if defined $_[0]->{namespace};
71 $_[0]->{namespace} = \%{$_[0]->name . '::'};
74 weaken($_[0]->{namespace});
76 return $_[0]->{namespace};
89 sub _deconstruct_variable_name {
90 my ($self, $variable) = @_;
93 if (ref($variable) eq 'HASH') {
94 @ret = @{$variable}{qw[name sigil type]};
97 (defined $variable && length $variable)
98 || confess "You must pass a variable name";
100 my $sigil = substr($variable, 0, 1, '');
102 if (exists $SIGIL_MAP{$sigil}) {
103 @ret = ($variable, $sigil, $SIGIL_MAP{$sigil});
106 @ret = ("${sigil}${variable}", '', $SIGIL_MAP{''});
110 # XXX in pure perl, this will access things in inner packages,
111 # in xs, this will segfault - probably look more into this at
114 || confess "Variable names may not contain ::";
120 sub _valid_for_type {
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;
128 my $ref = reftype($value);
129 return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE' || $ref eq 'REGEXP' || $ref eq 'VSTRING';
134 my ($self, $variable, $initial_value, %opts) = @_;
136 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
139 $self->_valid_for_type($initial_value, $type)
140 || confess "$initial_value is not of type $type";
142 # cheap fail-fast check for PERLDBf_SUBLINE and '&'
143 if ($^P and $^P & 0x10 && $sigil eq '&') {
144 my $filename = $opts{filename};
145 my $first_line_num = $opts{first_line_num};
147 (undef, $filename, $first_line_num) = caller
148 if not defined $filename;
150 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
152 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
153 $DB::sub{$self->name . '::' . $name} = "$filename:$first_line_num-$last_line_num";
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;
164 my ($self, $name) = @_;
165 delete $self->namespace->{$name};
169 my ($self, $variable) = @_;
171 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
173 my $namespace = $self->namespace;
175 return unless exists $namespace->{$name};
177 my $entry_ref = \$namespace->{$name};
178 if (reftype($entry_ref) eq 'GLOB') {
179 if ($type eq 'SCALAR') {
180 if (BROKEN_SCALAR_INITIALIZATION) {
181 return defined ${ *{$entry_ref}{$type} };
184 return B::svref_2object($entry_ref)->SV->isa('B::SV');
188 return defined *{$entry_ref}{$type};
192 # a symbol table entry can be -1 (stub), string (stub with prototype),
193 # or reference (constant)
194 return $type eq 'CODE';
199 my ($self, $variable, %opts) = @_;
201 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
203 my $namespace = $self->namespace;
205 if (!exists $namespace->{$name}) {
207 if ($type eq 'ARRAY') {
208 if (BROKEN_ISA_ASSIGNMENT) {
211 $name eq 'ISA' ? () : ([])
215 $self->add_symbol($variable, []);
218 elsif ($type eq 'HASH') {
219 $self->add_symbol($variable, {});
221 elsif ($type eq 'SCALAR') {
222 $self->add_symbol($variable);
224 elsif ($type eq 'IO') {
225 $self->add_symbol($variable, Symbol::geniosym);
227 elsif ($type eq 'CODE') {
228 confess "Don't know how to vivify CODE variables";
231 confess "Unknown type $type in vivication";
239 my $entry_ref = \$namespace->{$name};
241 if (ref($entry_ref) eq 'GLOB') {
242 return *{$entry_ref}{$type};
245 if ($type eq 'CODE') {
247 return \&{ $self->name . '::' . $name };
255 sub get_or_add_symbol {
257 $self->get_symbol(@_, vivify => 1);
261 my ($self, $variable) = @_;
263 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
266 # no doubt this is grossly inefficient and
267 # could be done much easier and faster in XS
269 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
270 { sigil => '$', type => 'SCALAR', name => $name },
271 { sigil => '@', type => 'ARRAY', name => $name },
272 { sigil => '%', type => 'HASH', name => $name },
273 { sigil => '&', type => 'CODE', name => $name },
274 { sigil => '', type => 'IO', name => $name },
277 my ($scalar, $array, $hash, $code, $io);
278 if ($type eq 'SCALAR') {
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);
284 elsif ($type eq 'ARRAY') {
285 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
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);
290 elsif ($type eq 'HASH') {
291 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
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);
296 elsif ($type eq 'CODE') {
297 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
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);
302 elsif ($type eq 'IO') {
303 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
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);
309 confess "This should never ever ever happen";
312 $self->remove_glob($name);
314 $self->add_symbol($scalar_desc => $scalar) if defined $scalar;
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;
321 sub list_all_symbols {
322 my ($self, $type_filter) = @_;
324 my $namespace = $self->namespace;
325 return keys %{$namespace} unless defined $type_filter;
328 # or we can filter based on
329 # type (SCALAR|ARRAY|HASH|CODE)
330 if ($type_filter eq 'CODE') {
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
335 || defined(*{$namespace->{$_}}{CODE})
336 } keys %{$namespace};
338 elsif ($type_filter eq 'SCALAR') {
340 BROKEN_SCALAR_INITIALIZATION
341 ? (ref(\$namespace->{$_}) eq 'GLOB'
342 && defined(${*{$namespace->{$_}}{'SCALAR'}}))
344 my $entry = \$namespace->{$_};
345 ref($entry) eq 'GLOB'
346 && B::svref_2object($entry)->SV->isa('B::SV')
348 } keys %{$namespace};
352 ref(\$namespace->{$_}) eq 'GLOB'
353 && defined(*{$namespace->{$_}}{$type_filter})
354 } keys %{$namespace};
358 sub get_all_symbols {
359 my ($self, $type_filter) = @_;
361 my $namespace = $self->namespace;
362 return { %{$namespace} } unless defined $type_filter;
365 map { $_ => $self->get_symbol({name => $_, type => $type_filter}) }
366 $self->list_all_symbols($type_filter)
374 =item * remove_symbol also replaces the associated typeglob
376 This can cause unexpected behavior when doing manipulation at compile time -
377 removing subroutines will still allow them to be called from within the package
378 as subroutines (although they will not be available as methods). This can be
379 considered a feature in some cases (this is how L<namespace::clean> works, for
380 instance), but should not be relied upon - use C<remove_glob> directly if you
383 =item * Some minor memory leaks
385 The pure perl implementation has a couple minor memory leaks (see the TODO
386 tests in t/20-leaks.t) that I'm having a hard time tracking down - these may be
387 core perl bugs, it's hard to tell.
391 Please report any bugs through RT: email
392 C<bug-package-stash at rt.cpan.org>, or browse to
393 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
399 =item * L<Class::MOP::Package>
401 This module is a factoring out of code that used to live here
407 You can find this documentation for this module with the perldoc command.
409 perldoc Package::Stash
411 You can also look for information at:
415 =item * AnnoCPAN: Annotated CPAN documentation
417 L<http://annocpan.org/dist/Package-Stash>
421 L<http://cpanratings.perl.org/d/Package-Stash>
423 =item * RT: CPAN's request tracker
425 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
429 L<http://search.cpan.org/dist/Package-Stash>
435 Jesse Luehrs <doy at tozt dot net>
437 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
442 BROKEN_ISA_ASSIGNMENT