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) && reftype($package) ne 'HASH')) {
35 confess "Package::Stash->new must be passed the name of the "
36 . "package to access";
38 elsif (ref($package) && reftype($package) eq 'HASH') {
40 'namespace' => $package,
43 elsif ($package =~ /\A[0-9A-Z_a-z]+(?:::[0-9A-Z_a-z]+)*\z/) {
45 'package' => $package,
49 confess "$package is not a module name";
55 confess "Can't call name as a class method"
56 unless blessed($_[0]);
57 confess "Can't get the name of an anonymous package"
58 unless defined($_[0]->{package});
59 return $_[0]->{package};
63 confess "Can't call namespace as a class method"
64 unless blessed($_[0]);
66 if (BROKEN_WEAK_STASH) {
68 return \%{$_[0]->name . '::'};
71 return $_[0]->{namespace} if defined $_[0]->{namespace};
75 $_[0]->{namespace} = \%{$_[0]->name . '::'};
78 weaken($_[0]->{namespace});
80 return $_[0]->{namespace};
93 sub _deconstruct_variable_name {
94 my ($self, $variable) = @_;
97 if (ref($variable) eq 'HASH') {
98 @ret = @{$variable}{qw[name sigil type]};
101 (defined $variable && length $variable)
102 || confess "You must pass a variable name";
104 my $sigil = substr($variable, 0, 1, '');
106 if (exists $SIGIL_MAP{$sigil}) {
107 @ret = ($variable, $sigil, $SIGIL_MAP{$sigil});
110 @ret = ("${sigil}${variable}", '', $SIGIL_MAP{''});
114 # XXX in pure perl, this will access things in inner packages,
115 # in xs, this will segfault - probably look more into this at
118 || confess "Variable names may not contain ::";
124 sub _valid_for_type {
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;
132 my $ref = reftype($value);
133 return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE' || $ref eq 'REGEXP' || $ref eq 'VSTRING';
138 my ($self, $variable, $initial_value, %opts) = @_;
140 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
143 $self->_valid_for_type($initial_value, $type)
144 || confess "$initial_value is not of type $type";
146 # cheap fail-fast check for PERLDBf_SUBLINE and '&'
147 if ($^P and $^P & 0x10 && $sigil eq '&') {
148 my $filename = $opts{filename};
149 my $first_line_num = $opts{first_line_num};
151 (undef, $filename, $first_line_num) = caller
152 if not defined $filename;
154 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
156 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
157 $DB::sub{$self->name . '::' . $name} = "$filename:$first_line_num-$last_line_num";
161 my $namespace = $self->namespace;
162 $namespace->{$name} ||= *{ Symbol::gensym() };
165 *{ $namespace->{$name} } = ref $initial_value
166 ? $initial_value : \$initial_value;
171 my ($self, $name) = @_;
172 delete $self->namespace->{$name};
176 my ($self, $variable) = @_;
178 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
180 my $namespace = $self->namespace;
182 return unless exists $namespace->{$name};
184 my $entry_ref = \$namespace->{$name};
185 if (reftype($entry_ref) eq 'GLOB') {
186 if ($type eq 'SCALAR') {
187 if (BROKEN_SCALAR_INITIALIZATION) {
188 return defined ${ *{$entry_ref}{$type} };
191 return B::svref_2object($entry_ref)->SV->isa('B::SV');
195 return defined *{$entry_ref}{$type};
199 # a symbol table entry can be -1 (stub), string (stub with prototype),
200 # or reference (constant)
201 return $type eq 'CODE';
206 my ($self, $variable, %opts) = @_;
208 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
210 my $namespace = $self->namespace;
212 if (!exists $namespace->{$name}) {
214 if ($type eq 'ARRAY') {
215 if (BROKEN_ISA_ASSIGNMENT) {
218 $name eq 'ISA' ? () : ([])
222 $self->add_symbol($variable, []);
225 elsif ($type eq 'HASH') {
226 $self->add_symbol($variable, {});
228 elsif ($type eq 'SCALAR') {
229 $self->add_symbol($variable);
231 elsif ($type eq 'IO') {
232 $self->add_symbol($variable, Symbol::geniosym);
234 elsif ($type eq 'CODE') {
235 confess "Don't know how to vivify CODE variables";
238 confess "Unknown type $type in vivication";
246 my $entry_ref = \$namespace->{$name};
248 if (ref($entry_ref) eq 'GLOB') {
249 return *{$entry_ref}{$type};
252 if ($type eq 'CODE') {
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};
262 return \&{ $self->name . '::' . $name };
271 sub get_or_add_symbol {
273 $self->get_symbol(@_, vivify => 1);
277 my ($self, $variable) = @_;
279 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
282 # no doubt this is grossly inefficient and
283 # could be done much easier and faster in XS
285 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
286 { sigil => '$', type => 'SCALAR', name => $name },
287 { sigil => '@', type => 'ARRAY', name => $name },
288 { sigil => '%', type => 'HASH', name => $name },
289 { sigil => '&', type => 'CODE', name => $name },
290 { sigil => '', type => 'IO', name => $name },
293 my ($scalar, $array, $hash, $code, $io);
294 if ($type eq 'SCALAR') {
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);
300 elsif ($type eq 'ARRAY') {
301 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
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);
306 elsif ($type eq 'HASH') {
307 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
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);
312 elsif ($type eq 'CODE') {
313 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
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);
318 elsif ($type eq 'IO') {
319 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
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);
325 confess "This should never ever ever happen";
328 $self->remove_glob($name);
330 $self->add_symbol($scalar_desc => $scalar) if defined $scalar;
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;
337 sub list_all_symbols {
338 my ($self, $type_filter) = @_;
340 my $namespace = $self->namespace;
341 return keys %{$namespace} unless defined $type_filter;
344 # or we can filter based on
345 # type (SCALAR|ARRAY|HASH|CODE)
346 if ($type_filter eq 'CODE') {
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
351 || defined(*{$namespace->{$_}}{CODE})
352 } keys %{$namespace};
354 elsif ($type_filter eq 'SCALAR') {
356 BROKEN_SCALAR_INITIALIZATION
357 ? (ref(\$namespace->{$_}) eq 'GLOB'
358 && defined(${*{$namespace->{$_}}{'SCALAR'}}))
360 my $entry = \$namespace->{$_};
361 ref($entry) eq 'GLOB'
362 && B::svref_2object($entry)->SV->isa('B::SV')
364 } keys %{$namespace};
368 ref(\$namespace->{$_}) eq 'GLOB'
369 && defined(*{$namespace->{$_}}{$type_filter})
370 } keys %{$namespace};
374 sub get_all_symbols {
375 my ($self, $type_filter) = @_;
377 my $namespace = $self->namespace;
378 return { %{$namespace} } unless defined $type_filter;
381 map { $_ => $self->get_symbol({name => $_, type => $type_filter}) }
382 $self->list_all_symbols($type_filter)
390 =item * remove_symbol also replaces the associated typeglob
392 This can cause unexpected behavior when doing manipulation at compile time -
393 removing subroutines will still allow them to be called from within the package
394 as subroutines (although they will not be available as methods). This can be
395 considered a feature in some cases (this is how L<namespace::clean> works, for
396 instance), but should not be relied upon - use C<remove_glob> directly if you
399 =item * Some minor memory leaks
401 The pure perl implementation has a couple minor memory leaks (see the TODO
402 tests in t/20-leaks.t) that I'm having a hard time tracking down - these may be
403 core perl bugs, it's hard to tell.
407 Please report any bugs through RT: email
408 C<bug-package-stash at rt.cpan.org>, or browse to
409 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
415 =item * L<Class::MOP::Package>
417 This module is a factoring out of code that used to live here
423 You can find this documentation for this module with the perldoc command.
425 perldoc Package::Stash
427 You can also look for information at:
431 =item * AnnoCPAN: Annotated CPAN documentation
433 L<http://annocpan.org/dist/Package-Stash>
437 L<http://cpanratings.perl.org/d/Package-Stash>
439 =item * RT: CPAN's request tracker
441 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
445 L<http://search.cpan.org/dist/Package-Stash>
451 Jesse Luehrs <doy at tozt dot net>
453 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
458 BROKEN_ISA_ASSIGNMENT