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() };
163 *{ $namespace->{$name} } = ref $initial_value
164 ? $initial_value : \$initial_value;
168 my ($self, $name) = @_;
169 delete $self->namespace->{$name};
173 my ($self, $variable) = @_;
175 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
177 my $namespace = $self->namespace;
179 return unless exists $namespace->{$name};
181 my $entry_ref = \$namespace->{$name};
182 if (reftype($entry_ref) eq 'GLOB') {
183 if ($type eq 'SCALAR') {
184 if (BROKEN_SCALAR_INITIALIZATION) {
185 return defined ${ *{$entry_ref}{$type} };
188 return B::svref_2object($entry_ref)->SV->isa('B::SV');
192 return defined *{$entry_ref}{$type};
196 # a symbol table entry can be -1 (stub), string (stub with prototype),
197 # or reference (constant)
198 return $type eq 'CODE';
203 my ($self, $variable, %opts) = @_;
205 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
207 my $namespace = $self->namespace;
209 if (!exists $namespace->{$name}) {
211 if ($type eq 'ARRAY') {
212 if (BROKEN_ISA_ASSIGNMENT) {
215 $name eq 'ISA' ? () : ([])
219 $self->add_symbol($variable, []);
222 elsif ($type eq 'HASH') {
223 $self->add_symbol($variable, {});
225 elsif ($type eq 'SCALAR') {
226 $self->add_symbol($variable);
228 elsif ($type eq 'IO') {
229 $self->add_symbol($variable, Symbol::geniosym);
231 elsif ($type eq 'CODE') {
232 confess "Don't know how to vivify CODE variables";
235 confess "Unknown type $type in vivication";
243 my $entry_ref = \$namespace->{$name};
245 if (ref($entry_ref) eq 'GLOB') {
246 return *{$entry_ref}{$type};
249 if ($type eq 'CODE') {
250 # XXX we should really be able to support arbitrary anonymous
251 # stashes here... (not just via Package::Anon)
252 if (blessed($namespace) && $namespace->isa('Package::Anon')) {
253 # ->can will call gv_init for us
254 $namespace->bless(\(my $foo))->can($name);
255 return *{ $namespace->{$name} }{CODE};
259 return \&{ $self->name . '::' . $name };
268 sub get_or_add_symbol {
270 $self->get_symbol(@_, vivify => 1);
274 my ($self, $variable) = @_;
276 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
279 # no doubt this is grossly inefficient and
280 # could be done much easier and faster in XS
282 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
283 { sigil => '$', type => 'SCALAR', name => $name },
284 { sigil => '@', type => 'ARRAY', name => $name },
285 { sigil => '%', type => 'HASH', name => $name },
286 { sigil => '&', type => 'CODE', name => $name },
287 { sigil => '', type => 'IO', name => $name },
290 my ($scalar, $array, $hash, $code, $io);
291 if ($type eq 'SCALAR') {
292 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
293 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
294 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
295 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
297 elsif ($type eq 'ARRAY') {
298 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
299 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
300 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
301 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
303 elsif ($type eq 'HASH') {
304 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
305 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
306 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
307 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
309 elsif ($type eq 'CODE') {
310 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
311 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
312 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
313 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
315 elsif ($type eq 'IO') {
316 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
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);
322 confess "This should never ever ever happen";
325 $self->remove_glob($name);
327 $self->add_symbol($scalar_desc => $scalar) if defined $scalar;
328 $self->add_symbol($array_desc => $array) if defined $array;
329 $self->add_symbol($hash_desc => $hash) if defined $hash;
330 $self->add_symbol($code_desc => $code) if defined $code;
331 $self->add_symbol($io_desc => $io) if defined $io;
334 sub list_all_symbols {
335 my ($self, $type_filter) = @_;
337 my $namespace = $self->namespace;
338 return keys %{$namespace} unless defined $type_filter;
341 # or we can filter based on
342 # type (SCALAR|ARRAY|HASH|CODE)
343 if ($type_filter eq 'CODE') {
345 # any non-typeglob in the symbol table is a constant or stub
346 ref(\$namespace->{$_}) ne 'GLOB'
347 # regular subs are stored in the CODE slot of the typeglob
348 || defined(*{$namespace->{$_}}{CODE})
349 } keys %{$namespace};
351 elsif ($type_filter eq 'SCALAR') {
353 BROKEN_SCALAR_INITIALIZATION
354 ? (ref(\$namespace->{$_}) eq 'GLOB'
355 && defined(${*{$namespace->{$_}}{'SCALAR'}}))
357 my $entry = \$namespace->{$_};
358 ref($entry) eq 'GLOB'
359 && B::svref_2object($entry)->SV->isa('B::SV')
361 } keys %{$namespace};
365 ref(\$namespace->{$_}) eq 'GLOB'
366 && defined(*{$namespace->{$_}}{$type_filter})
367 } keys %{$namespace};
371 sub get_all_symbols {
372 my ($self, $type_filter) = @_;
374 my $namespace = $self->namespace;
375 return { %{$namespace} } unless defined $type_filter;
378 map { $_ => $self->get_symbol({name => $_, type => $type_filter}) }
379 $self->list_all_symbols($type_filter)
387 =item * remove_symbol also replaces the associated typeglob
389 This can cause unexpected behavior when doing manipulation at compile time -
390 removing subroutines will still allow them to be called from within the package
391 as subroutines (although they will not be available as methods). This can be
392 considered a feature in some cases (this is how L<namespace::clean> works, for
393 instance), but should not be relied upon - use C<remove_glob> directly if you
396 =item * Some minor memory leaks
398 The pure perl implementation has a couple minor memory leaks (see the TODO
399 tests in t/20-leaks.t) that I'm having a hard time tracking down - these may be
400 core perl bugs, it's hard to tell.
404 Please report any bugs through RT: email
405 C<bug-package-stash at rt.cpan.org>, or browse to
406 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
412 =item * L<Class::MOP::Package>
414 This module is a factoring out of code that used to live here
420 You can find this documentation for this module with the perldoc command.
422 perldoc Package::Stash
424 You can also look for information at:
428 =item * AnnoCPAN: Annotated CPAN documentation
430 L<http://annocpan.org/dist/Package-Stash>
434 L<http://cpanratings.perl.org/d/Package-Stash>
436 =item * RT: CPAN's request tracker
438 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
442 L<http://search.cpan.org/dist/Package-Stash>
448 Jesse Luehrs <doy at tozt dot net>
450 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
455 BROKEN_ISA_ASSIGNMENT