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);
19 # add_method on anon stashes triggers rt.perl #1804 otherwise
20 # fixed in perl commit v5.13.3-70-g0fe688f
21 use constant BROKEN_GLOB_ASSIGNMENT => ($] < 5.013004);
29 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.
37 if (!defined($package) || (ref($package) && reftype($package) ne 'HASH')) {
38 confess "Package::Stash->new must be passed the name of the "
39 . "package to access";
41 elsif (ref($package) && reftype($package) eq 'HASH') {
42 confess "The PP implementation of Package::Stash does not support "
43 . "anonymous stashes before perl 5.14"
44 if BROKEN_GLOB_ASSIGNMENT;
47 'namespace' => $package,
50 elsif ($package =~ /\A[0-9A-Z_a-z]+(?:::[0-9A-Z_a-z]+)*\z/) {
52 'package' => $package,
56 confess "$package is not a module name";
62 confess "Can't call name as a class method"
63 unless blessed($_[0]);
64 confess "Can't get the name of an anonymous package"
65 unless defined($_[0]->{package});
66 return $_[0]->{package};
70 confess "Can't call namespace as a class method"
71 unless blessed($_[0]);
73 if (BROKEN_WEAK_STASH) {
75 return \%{$_[0]->name . '::'};
78 return $_[0]->{namespace} if defined $_[0]->{namespace};
82 $_[0]->{namespace} = \%{$_[0]->name . '::'};
85 weaken($_[0]->{namespace});
87 return $_[0]->{namespace};
100 sub _deconstruct_variable_name {
101 my ($self, $variable) = @_;
104 if (ref($variable) eq 'HASH') {
105 @ret = @{$variable}{qw[name sigil type]};
108 (defined $variable && length $variable)
109 || confess "You must pass a variable name";
111 my $sigil = substr($variable, 0, 1, '');
113 if (exists $SIGIL_MAP{$sigil}) {
114 @ret = ($variable, $sigil, $SIGIL_MAP{$sigil});
117 @ret = ("${sigil}${variable}", '', $SIGIL_MAP{''});
121 # XXX in pure perl, this will access things in inner packages,
122 # in xs, this will segfault - probably look more into this at
125 || confess "Variable names may not contain ::";
131 sub _valid_for_type {
133 my ($value, $type) = @_;
134 if ($type eq 'HASH' || $type eq 'ARRAY'
135 || $type eq 'IO' || $type eq 'CODE') {
136 return reftype($value) eq $type;
139 my $ref = reftype($value);
140 return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE' || $ref eq 'REGEXP' || $ref eq 'VSTRING';
145 my ($self, $variable, $initial_value, %opts) = @_;
147 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
150 $self->_valid_for_type($initial_value, $type)
151 || confess "$initial_value is not of type $type";
153 # cheap fail-fast check for PERLDBf_SUBLINE and '&'
154 if ($^P and $^P & 0x10 && $sigil eq '&') {
155 my $filename = $opts{filename};
156 my $first_line_num = $opts{first_line_num};
158 (undef, $filename, $first_line_num) = caller
159 if not defined $filename;
161 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
163 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
164 $DB::sub{$self->name . '::' . $name} = "$filename:$first_line_num-$last_line_num";
168 if (BROKEN_GLOB_ASSIGNMENT) {
171 *{ $self->name . '::' . $name } = ref $initial_value
172 ? $initial_value : \$initial_value;
176 *{ $self->name . '::' . $name };
180 my $namespace = $self->namespace;
181 $namespace->{$name} ||= *{ Symbol::gensym() };
184 *{ $namespace->{$name} } = ref $initial_value
185 ? $initial_value : \$initial_value;
191 my ($self, $name) = @_;
192 delete $self->namespace->{$name};
196 my ($self, $variable) = @_;
198 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
200 my $namespace = $self->namespace;
202 return unless exists $namespace->{$name};
204 my $entry_ref = \$namespace->{$name};
205 if (reftype($entry_ref) eq 'GLOB') {
206 if ($type eq 'SCALAR') {
207 if (BROKEN_SCALAR_INITIALIZATION) {
208 return defined ${ *{$entry_ref}{$type} };
211 return B::svref_2object($entry_ref)->SV->isa('B::SV');
215 return defined *{$entry_ref}{$type};
219 # a symbol table entry can be -1 (stub), string (stub with prototype),
220 # or reference (constant)
221 return $type eq 'CODE';
226 my ($self, $variable, %opts) = @_;
228 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
230 my $namespace = $self->namespace;
232 if (!exists $namespace->{$name}) {
234 if ($type eq 'ARRAY') {
235 if (BROKEN_ISA_ASSIGNMENT) {
238 $name eq 'ISA' ? () : ([])
242 $self->add_symbol($variable, []);
245 elsif ($type eq 'HASH') {
246 $self->add_symbol($variable, {});
248 elsif ($type eq 'SCALAR') {
249 $self->add_symbol($variable);
251 elsif ($type eq 'IO') {
252 $self->add_symbol($variable, Symbol::geniosym);
254 elsif ($type eq 'CODE') {
255 confess "Don't know how to vivify CODE variables";
258 confess "Unknown type $type in vivication";
266 my $entry_ref = \$namespace->{$name};
268 if (ref($entry_ref) eq 'GLOB') {
269 return *{$entry_ref}{$type};
272 if ($type eq 'CODE') {
273 # XXX we should really be able to support arbitrary anonymous
274 # stashes here... (not just via Package::Anon)
275 if (!BROKEN_GLOB_ASSIGNMENT
276 && blessed($namespace)
277 && $namespace->isa('Package::Anon')) {
278 # ->can will call gv_init for us
279 $namespace->bless(\(my $foo))->can($name);
280 return *{ $namespace->{$name} }{CODE};
284 return \&{ $self->name . '::' . $name };
293 sub get_or_add_symbol {
295 $self->get_symbol(@_, vivify => 1);
299 my ($self, $variable) = @_;
301 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
304 # no doubt this is grossly inefficient and
305 # could be done much easier and faster in XS
307 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
308 { sigil => '$', type => 'SCALAR', name => $name },
309 { sigil => '@', type => 'ARRAY', name => $name },
310 { sigil => '%', type => 'HASH', name => $name },
311 { sigil => '&', type => 'CODE', name => $name },
312 { sigil => '', type => 'IO', name => $name },
315 my ($scalar, $array, $hash, $code, $io);
316 if ($type eq 'SCALAR') {
317 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
318 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
319 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
320 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
322 elsif ($type eq 'ARRAY') {
323 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
324 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
325 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
326 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
328 elsif ($type eq 'HASH') {
329 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
330 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
331 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
332 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
334 elsif ($type eq 'CODE') {
335 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
336 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
337 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
338 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
340 elsif ($type eq 'IO') {
341 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
342 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
343 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
344 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
347 confess "This should never ever ever happen";
350 $self->remove_glob($name);
352 $self->add_symbol($scalar_desc => $scalar) if defined $scalar;
353 $self->add_symbol($array_desc => $array) if defined $array;
354 $self->add_symbol($hash_desc => $hash) if defined $hash;
355 $self->add_symbol($code_desc => $code) if defined $code;
356 $self->add_symbol($io_desc => $io) if defined $io;
359 sub list_all_symbols {
360 my ($self, $type_filter) = @_;
362 my $namespace = $self->namespace;
363 return keys %{$namespace} unless defined $type_filter;
366 # or we can filter based on
367 # type (SCALAR|ARRAY|HASH|CODE)
368 if ($type_filter eq 'CODE') {
370 # any non-typeglob in the symbol table is a constant or stub
371 ref(\$namespace->{$_}) ne 'GLOB'
372 # regular subs are stored in the CODE slot of the typeglob
373 || defined(*{$namespace->{$_}}{CODE})
374 } keys %{$namespace};
376 elsif ($type_filter eq 'SCALAR') {
378 BROKEN_SCALAR_INITIALIZATION
379 ? (ref(\$namespace->{$_}) eq 'GLOB'
380 && defined(${*{$namespace->{$_}}{'SCALAR'}}))
382 my $entry = \$namespace->{$_};
383 ref($entry) eq 'GLOB'
384 && B::svref_2object($entry)->SV->isa('B::SV')
386 } keys %{$namespace};
390 ref(\$namespace->{$_}) eq 'GLOB'
391 && defined(*{$namespace->{$_}}{$type_filter})
392 } keys %{$namespace};
396 sub get_all_symbols {
397 my ($self, $type_filter) = @_;
399 my $namespace = $self->namespace;
400 return { %{$namespace} } unless defined $type_filter;
403 map { $_ => $self->get_symbol({name => $_, type => $type_filter}) }
404 $self->list_all_symbols($type_filter)
412 =item * remove_symbol also replaces the associated typeglob
414 This can cause unexpected behavior when doing manipulation at compile time -
415 removing subroutines will still allow them to be called from within the package
416 as subroutines (although they will not be available as methods). This can be
417 considered a feature in some cases (this is how L<namespace::clean> works, for
418 instance), but should not be relied upon - use C<remove_glob> directly if you
421 =item * Some minor memory leaks
423 The pure perl implementation has a couple minor memory leaks (see the TODO
424 tests in t/20-leaks.t) that I'm having a hard time tracking down - these may be
425 core perl bugs, it's hard to tell.
429 Please report any bugs through RT: email
430 C<bug-package-stash at rt.cpan.org>, or browse to
431 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
437 =item * L<Class::MOP::Package>
439 This module is a factoring out of code that used to live here
445 You can find this documentation for this module with the perldoc command.
447 perldoc Package::Stash
449 You can also look for information at:
453 =item * AnnoCPAN: Annotated CPAN documentation
455 L<http://annocpan.org/dist/Package-Stash>
459 L<http://cpanratings.perl.org/d/Package-Stash>
461 =item * RT: CPAN's request tracker
463 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
467 L<http://search.cpan.org/dist/Package-Stash>
473 Jesse Luehrs <doy at tozt dot net>
475 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
480 BROKEN_ISA_ASSIGNMENT