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};
92 return !defined $_[0]->{package};
104 sub _deconstruct_variable_name {
105 my ($self, $variable) = @_;
108 if (ref($variable) eq 'HASH') {
109 @ret = @{$variable}{qw[name sigil type]};
112 (defined $variable && length $variable)
113 || confess "You must pass a variable name";
115 my $sigil = substr($variable, 0, 1, '');
117 if (exists $SIGIL_MAP{$sigil}) {
118 @ret = ($variable, $sigil, $SIGIL_MAP{$sigil});
121 @ret = ("${sigil}${variable}", '', $SIGIL_MAP{''});
125 # XXX in pure perl, this will access things in inner packages,
126 # in xs, this will segfault - probably look more into this at
129 || confess "Variable names may not contain ::";
135 sub _valid_for_type {
137 my ($value, $type) = @_;
138 if ($type eq 'HASH' || $type eq 'ARRAY'
139 || $type eq 'IO' || $type eq 'CODE') {
140 return reftype($value) eq $type;
143 my $ref = reftype($value);
144 return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE' || $ref eq 'REGEXP' || $ref eq 'VSTRING';
149 my ($self, $variable, $initial_value, %opts) = @_;
151 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
154 $self->_valid_for_type($initial_value, $type)
155 || confess "$initial_value is not of type $type";
157 # cheap fail-fast check for PERLDBf_SUBLINE and '&'
158 if ($^P and $^P & 0x10 && $sigil eq '&') {
159 my $filename = $opts{filename};
160 my $first_line_num = $opts{first_line_num};
162 (undef, $filename, $first_line_num) = caller
163 if not defined $filename;
165 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
167 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
168 $DB::sub{$self->name . '::' . $name} = "$filename:$first_line_num-$last_line_num";
172 if (BROKEN_GLOB_ASSIGNMENT) {
175 *{ $self->name . '::' . $name } = ref $initial_value
176 ? $initial_value : \$initial_value;
180 *{ $self->name . '::' . $name };
184 my $namespace = $self->namespace;
185 $namespace->{$name} ||= *{ Symbol::gensym() };
188 *{ $namespace->{$name} } = ref $initial_value
189 ? $initial_value : \$initial_value;
195 my ($self, $name) = @_;
196 delete $self->namespace->{$name};
200 my ($self, $variable) = @_;
202 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
204 my $namespace = $self->namespace;
206 return unless exists $namespace->{$name};
208 my $entry_ref = \$namespace->{$name};
209 if (reftype($entry_ref) eq 'GLOB') {
210 if ($type eq 'SCALAR') {
211 if (BROKEN_SCALAR_INITIALIZATION) {
212 return defined ${ *{$entry_ref}{$type} };
215 return B::svref_2object($entry_ref)->SV->isa('B::SV');
219 return defined *{$entry_ref}{$type};
223 # a symbol table entry can be -1 (stub), string (stub with prototype),
224 # or reference (constant)
225 return $type eq 'CODE';
230 my ($self, $variable, %opts) = @_;
232 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
234 my $namespace = $self->namespace;
236 if (!exists $namespace->{$name}) {
238 if ($type eq 'ARRAY') {
239 if (BROKEN_ISA_ASSIGNMENT) {
242 $name eq 'ISA' ? () : ([])
246 $self->add_symbol($variable, []);
249 elsif ($type eq 'HASH') {
250 $self->add_symbol($variable, {});
252 elsif ($type eq 'SCALAR') {
253 $self->add_symbol($variable);
255 elsif ($type eq 'IO') {
256 $self->add_symbol($variable, Symbol::geniosym);
258 elsif ($type eq 'CODE') {
259 confess "Don't know how to vivify CODE variables";
262 confess "Unknown type $type in vivication";
270 my $entry_ref = \$namespace->{$name};
272 if (ref($entry_ref) eq 'GLOB') {
273 return *{$entry_ref}{$type};
276 if ($type eq 'CODE') {
277 if (BROKEN_GLOB_ASSIGNMENT || !$self->_is_anon) {
279 return \&{ $self->name . '::' . $name };
282 # XXX we should really be able to support arbitrary anonymous
283 # stashes here... (not just via Package::Anon)
284 if (blessed($namespace) && $namespace->isa('Package::Anon')) {
285 # ->can will call gv_init for us, which inflates the glob
286 # don't know how to do this in general
287 $namespace->bless(\(my $foo))->can($name);
290 confess "Don't know how to inflate a " . ref($entry_ref)
291 . " into a full coderef (perhaps you could use"
292 . " Package::Anon instead of a bare stash?)"
295 return *{ $namespace->{$name} }{CODE};
303 sub get_or_add_symbol {
305 $self->get_symbol(@_, vivify => 1);
309 my ($self, $variable) = @_;
311 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
314 # no doubt this is grossly inefficient and
315 # could be done much easier and faster in XS
317 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
318 { sigil => '$', type => 'SCALAR', name => $name },
319 { sigil => '@', type => 'ARRAY', name => $name },
320 { sigil => '%', type => 'HASH', name => $name },
321 { sigil => '&', type => 'CODE', name => $name },
322 { sigil => '', type => 'IO', name => $name },
325 my ($scalar, $array, $hash, $code, $io);
326 if ($type eq 'SCALAR') {
327 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
328 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
329 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
330 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
332 elsif ($type eq 'ARRAY') {
333 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
334 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
335 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
336 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
338 elsif ($type eq 'HASH') {
339 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
340 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
341 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
342 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
344 elsif ($type eq 'CODE') {
345 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
346 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
347 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
348 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
350 elsif ($type eq 'IO') {
351 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
352 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
353 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
354 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
357 confess "This should never ever ever happen";
360 $self->remove_glob($name);
362 $self->add_symbol($scalar_desc => $scalar) if defined $scalar;
363 $self->add_symbol($array_desc => $array) if defined $array;
364 $self->add_symbol($hash_desc => $hash) if defined $hash;
365 $self->add_symbol($code_desc => $code) if defined $code;
366 $self->add_symbol($io_desc => $io) if defined $io;
369 sub list_all_symbols {
370 my ($self, $type_filter) = @_;
372 my $namespace = $self->namespace;
373 return keys %{$namespace} unless defined $type_filter;
376 # or we can filter based on
377 # type (SCALAR|ARRAY|HASH|CODE)
378 if ($type_filter eq 'CODE') {
380 # any non-typeglob in the symbol table is a constant or stub
381 ref(\$namespace->{$_}) ne 'GLOB'
382 # regular subs are stored in the CODE slot of the typeglob
383 || defined(*{$namespace->{$_}}{CODE})
384 } keys %{$namespace};
386 elsif ($type_filter eq 'SCALAR') {
388 BROKEN_SCALAR_INITIALIZATION
389 ? (ref(\$namespace->{$_}) eq 'GLOB'
390 && defined(${*{$namespace->{$_}}{'SCALAR'}}))
392 my $entry = \$namespace->{$_};
393 ref($entry) eq 'GLOB'
394 && B::svref_2object($entry)->SV->isa('B::SV')
396 } keys %{$namespace};
400 ref(\$namespace->{$_}) eq 'GLOB'
401 && defined(*{$namespace->{$_}}{$type_filter})
402 } keys %{$namespace};
406 sub get_all_symbols {
407 my ($self, $type_filter) = @_;
409 my $namespace = $self->namespace;
410 return { %{$namespace} } unless defined $type_filter;
413 map { $_ => $self->get_symbol({name => $_, type => $type_filter}) }
414 $self->list_all_symbols($type_filter)
422 =item * remove_symbol also replaces the associated typeglob
424 This can cause unexpected behavior when doing manipulation at compile time -
425 removing subroutines will still allow them to be called from within the package
426 as subroutines (although they will not be available as methods). This can be
427 considered a feature in some cases (this is how L<namespace::clean> works, for
428 instance), but should not be relied upon - use C<remove_glob> directly if you
431 =item * Some minor memory leaks
433 The pure perl implementation has a couple minor memory leaks (see the TODO
434 tests in t/20-leaks.t) that I'm having a hard time tracking down - these may be
435 core perl bugs, it's hard to tell.
439 Please report any bugs through RT: email
440 C<bug-package-stash at rt.cpan.org>, or browse to
441 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
447 =item * L<Class::MOP::Package>
449 This module is a factoring out of code that used to live here
455 You can find this documentation for this module with the perldoc command.
457 perldoc Package::Stash
459 You can also look for information at:
463 =item * AnnoCPAN: Annotated CPAN documentation
465 L<http://annocpan.org/dist/Package-Stash>
469 L<http://cpanratings.perl.org/d/Package-Stash>
471 =item * RT: CPAN's request tracker
473 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
477 L<http://search.cpan.org/dist/Package-Stash>
483 Jesse Luehrs <doy at tozt dot net>
485 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
490 BROKEN_ISA_ASSIGNMENT