1 package Package::Stash::PP;
4 # ABSTRACT: pure perl implementation of the Package::Stash API
7 use Scalar::Util qw(blessed reftype weaken);
9 # before 5.12, assigning to the ISA glob would make it lose its magical ->isa
11 use constant BROKEN_ISA_ASSIGNMENT => ($] < 5.012);
12 # before 5.10, stashes don't ever seem to drop to a refcount of zero, so
13 # weakening them isn't helpful
14 use constant BROKEN_WEAK_STASH => ($] < 5.010);
22 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.
30 if (!defined($package) || (ref($package) && ref($package) ne 'HASH')) {
31 confess "Package::Stash->new must be passed the name of the "
32 . "package to access";
34 elsif (ref($package) eq 'HASH') {
35 confess "The pure perl implementation of Package::Stash doesn't "
36 . "currently support anonymous stashes. You should install "
37 . "Package::Stash::XS";
41 'package' => $package,
46 confess "Can't call name as a class method"
47 unless blessed($_[0]);
48 return $_[0]->{package};
52 confess "Can't call namespace as a class method"
53 unless blessed($_[0]);
55 if (BROKEN_WEAK_STASH) {
57 return \%{$_[0]->name . '::'};
60 return $_[0]->{namespace} if defined $_[0]->{namespace};
64 $_[0]->{namespace} = \%{$_[0]->name . '::'};
67 weaken($_[0]->{namespace});
69 return $_[0]->{namespace};
82 sub _deconstruct_variable_name {
83 my ($self, $variable) = @_;
85 (defined $variable && length $variable)
86 || confess "You must pass a variable name";
88 my $sigil = substr($variable, 0, 1, '');
90 if (exists $SIGIL_MAP{$sigil}) {
91 return ($variable, $sigil, $SIGIL_MAP{$sigil});
94 return ("${sigil}${variable}", '', $SIGIL_MAP{''});
101 my ($value, $type) = @_;
102 if ($type eq 'HASH' || $type eq 'ARRAY'
103 || $type eq 'IO' || $type eq 'CODE') {
104 return reftype($value) eq $type;
107 my $ref = reftype($value);
108 return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE' || $ref eq 'REGEXP' || $ref eq 'VSTRING';
113 my ($self, $variable, $initial_value, %opts) = @_;
115 my ($name, $sigil, $type) = ref $variable eq 'HASH'
116 ? @{$variable}{qw[name sigil type]}
117 : $self->_deconstruct_variable_name($variable);
119 my $pkg = $self->name;
122 $self->_valid_for_type($initial_value, $type)
123 || confess "$initial_value is not of type $type";
125 # cheap fail-fast check for PERLDBf_SUBLINE and '&'
126 if ($^P and $^P & 0x10 && $sigil eq '&') {
127 my $filename = $opts{filename};
128 my $first_line_num = $opts{first_line_num};
130 (undef, $filename, $first_line_num) = caller
131 if not defined $filename;
133 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
135 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
136 $DB::sub{$pkg . '::' . $name} = "$filename:$first_line_num-$last_line_num";
141 no warnings 'redefine', 'misc', 'prototype';
142 *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
146 my ($self, $name) = @_;
147 delete $self->namespace->{$name};
151 my ($self, $variable) = @_;
153 my ($name, $sigil, $type) = ref $variable eq 'HASH'
154 ? @{$variable}{qw[name sigil type]}
155 : $self->_deconstruct_variable_name($variable);
157 my $namespace = $self->namespace;
159 return unless exists $namespace->{$name};
161 my $entry_ref = \$namespace->{$name};
162 if (reftype($entry_ref) eq 'GLOB') {
163 # XXX: assigning to any typeglob slot also initializes the SCALAR slot,
164 # and saying that an undef scalar variable doesn't exist is probably
165 # vaguely less surprising than a scalar variable popping into existence
166 # without anyone defining it
167 if ($type eq 'SCALAR') {
168 return defined ${ *{$entry_ref}{$type} };
171 return defined *{$entry_ref}{$type};
175 # a symbol table entry can be -1 (stub), string (stub with prototype),
176 # or reference (constant)
177 return $type eq 'CODE';
182 my ($self, $variable, %opts) = @_;
184 my ($name, $sigil, $type) = ref $variable eq 'HASH'
185 ? @{$variable}{qw[name sigil type]}
186 : $self->_deconstruct_variable_name($variable);
188 my $namespace = $self->namespace;
190 if (!exists $namespace->{$name}) {
192 if ($type eq 'ARRAY') {
193 if (BROKEN_ISA_ASSIGNMENT) {
196 $name eq 'ISA' ? () : ([])
200 $self->add_symbol($variable, []);
203 elsif ($type eq 'HASH') {
204 $self->add_symbol($variable, {});
206 elsif ($type eq 'SCALAR') {
207 $self->add_symbol($variable);
209 elsif ($type eq 'IO') {
210 $self->add_symbol($variable, Symbol::geniosym);
212 elsif ($type eq 'CODE') {
213 confess "Don't know how to vivify CODE variables";
216 confess "Unknown type $type in vivication";
224 my $entry_ref = \$namespace->{$name};
226 if (ref($entry_ref) eq 'GLOB') {
227 return *{$entry_ref}{$type};
230 if ($type eq 'CODE') {
232 return \&{ $self->name . '::' . $name };
240 sub get_or_add_symbol {
242 $self->get_symbol(@_, vivify => 1);
246 my ($self, $variable) = @_;
248 my ($name, $sigil, $type) = ref $variable eq 'HASH'
249 ? @{$variable}{qw[name sigil type]}
250 : $self->_deconstruct_variable_name($variable);
253 # no doubt this is grossly inefficient and
254 # could be done much easier and faster in XS
256 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
257 { sigil => '$', type => 'SCALAR', name => $name },
258 { sigil => '@', type => 'ARRAY', name => $name },
259 { sigil => '%', type => 'HASH', name => $name },
260 { sigil => '&', type => 'CODE', name => $name },
261 { sigil => '', type => 'IO', name => $name },
264 my ($scalar, $array, $hash, $code, $io);
265 if ($type eq 'SCALAR') {
266 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
267 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
268 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
269 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
271 elsif ($type eq 'ARRAY') {
272 $scalar = $self->get_symbol($scalar_desc);
273 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
274 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
275 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
277 elsif ($type eq 'HASH') {
278 $scalar = $self->get_symbol($scalar_desc);
279 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
280 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
281 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
283 elsif ($type eq 'CODE') {
284 $scalar = $self->get_symbol($scalar_desc);
285 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
286 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
287 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
289 elsif ($type eq 'IO') {
290 $scalar = $self->get_symbol($scalar_desc);
291 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
292 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
293 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
296 confess "This should never ever ever happen";
299 $self->remove_glob($name);
301 $self->add_symbol($scalar_desc => $scalar);
302 $self->add_symbol($array_desc => $array) if defined $array;
303 $self->add_symbol($hash_desc => $hash) if defined $hash;
304 $self->add_symbol($code_desc => $code) if defined $code;
305 $self->add_symbol($io_desc => $io) if defined $io;
308 sub list_all_symbols {
309 my ($self, $type_filter) = @_;
311 my $namespace = $self->namespace;
312 return keys %{$namespace} unless defined $type_filter;
315 # or we can filter based on
316 # type (SCALAR|ARRAY|HASH|CODE)
317 if ($type_filter eq 'CODE') {
319 # any non-typeglob in the symbol table is a constant or stub
320 ref(\$namespace->{$_}) ne 'GLOB'
321 # regular subs are stored in the CODE slot of the typeglob
322 || defined(*{$namespace->{$_}}{CODE})
323 } keys %{$namespace};
325 elsif ($type_filter eq 'SCALAR') {
327 ref(\$namespace->{$_}) eq 'GLOB'
328 && defined(${*{$namespace->{$_}}{'SCALAR'}})
329 } keys %{$namespace};
333 ref(\$namespace->{$_}) eq 'GLOB'
334 && defined(*{$namespace->{$_}}{$type_filter})
335 } keys %{$namespace};
339 sub get_all_symbols {
340 my ($self, $type_filter) = @_;
342 my $namespace = $self->namespace;
343 return { %{$namespace} } unless defined $type_filter;
346 map { $_ => $self->get_symbol({name => $_, type => $type_filter}) }
347 $self->list_all_symbols($type_filter)
355 =item * Scalar slots are only considered to exist if they are defined
357 This is due to a shortcoming within perl itself. See
358 L<perlref/Making References> point 7 for more information.
360 =item * remove_symbol also replaces the associated typeglob
362 This can cause unexpected behavior when doing manipulation at compile time -
363 removing subroutines will still allow them to be called from within the package
364 as subroutines (although they will not be available as methods). This can be
365 considered a feature in some cases (this is how L<namespace::clean> works, for
366 instance), but should not be relied upon - use C<remove_glob> directly if you
369 =item * Some minor memory leaks
371 The pure perl implementation has a couple minor memory leaks (see the TODO
372 tests in t/20-leaks.t) that I'm having a hard time tracking down - these may be
373 core perl bugs, it's hard to tell.
377 Please report any bugs through RT: email
378 C<bug-package-stash at rt.cpan.org>, or browse to
379 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
385 =item * L<Class::MOP::Package>
387 This module is a factoring out of code that used to live here
393 You can find this documentation for this module with the perldoc command.
395 perldoc Package::Stash
397 You can also look for information at:
401 =item * AnnoCPAN: Annotated CPAN documentation
403 L<http://annocpan.org/dist/Package-Stash>
407 L<http://cpanratings.perl.org/d/Package-Stash>
409 =item * RT: CPAN's request tracker
411 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
415 L<http://search.cpan.org/dist/Package-Stash>
421 Jesse Luehrs <doy at tozt dot net>
423 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
428 BROKEN_ISA_ASSIGNMENT