Revision history for Perl extension Class-MOP.
+0.57
+ * Class::MOP::Class
+ - made get_method_map use list_all_package_symbols
+ instead of manually grabbing each symbol
+
+ * Class::MOP::Package
+ - made {get, has}_package_symbol not call
+ &namespace so much
+ - added get_all_package_symbols to fetch
+ a HASH of items based on a type filter
+ similar to list_all_package_symbols
+ - added tests for this
+
0.56 Saturday, May 24, 2008
* Class::MOP
- we now get the &check_package_cache_flag
use Carp 'confess';
use Scalar::Util 'blessed', 'reftype', 'weaken';
-our $VERSION = '0.31';
+our $VERSION = '0.32';
our $AUTHORITY = 'cpan:STEVAN';
use base 'Class::MOP::Module';
my $class_name = $self->name;
my $method_metaclass = $self->method_metaclass;
- foreach my $symbol ($self->list_all_package_symbols('CODE')) {
- my $code = $self->get_package_symbol('&' . $symbol);
+ my %all_code = $self->get_all_package_symbols('CODE');
+
+ foreach my $symbol (keys %all_code) {
+ my $code = $all_code{$symbol};
next if exists $map->{$symbol} &&
defined $map->{$symbol} &&
use Scalar::Util 'blessed';
use Carp 'confess';
-our $VERSION = '0.08';
+our $VERSION = '0.09';
our $AUTHORITY = 'cpan:STEVAN';
use base 'Class::MOP::Object';
my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
- return 0 unless exists $self->namespace->{$name};
+ my $namespace = $self->namespace;
+
+ return 0 unless exists $namespace->{$name};
# FIXME:
# For some really stupid reason
# if you put \undef in your scalar
# then this is broken.
- if (ref($self->namespace->{$name}) eq 'SCALAR') {
+ if (ref($namespace->{$name}) eq 'SCALAR') {
return ($type eq 'CODE' ? 1 : 0);
}
elsif ($type eq 'SCALAR') {
- my $val = *{$self->namespace->{$name}}{$type};
+ my $val = *{$namespace->{$name}}{$type};
return defined(${$val}) ? 1 : 0;
}
else {
- defined(*{$self->namespace->{$name}}{$type}) ? 1 : 0;
+ defined(*{$namespace->{$name}}{$type}) ? 1 : 0;
}
}
my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
+ my $namespace = $self->namespace;
+
$self->add_package_symbol($variable)
- unless exists $self->namespace->{$name};
+ unless exists $namespace->{$name};
- if (ref($self->namespace->{$name}) eq 'SCALAR') {
+ if (ref($namespace->{$name}) eq 'SCALAR') {
if ($type eq 'CODE') {
no strict 'refs';
return \&{$self->name.'::'.$name};
}
}
else {
- return *{$self->namespace->{$name}}{$type};
+ return *{$namespace->{$name}}{$type};
}
}
} keys %{$namespace};
}
+sub get_all_package_symbols {
+ my ($self, $type_filter) = @_;
+ return %{$self->namespace} unless defined $type_filter;
+ # NOTE:
+ # or we can filter based on
+ # type (SCALAR|ARRAY|HASH|CODE)
+ my $namespace = $self->namespace;
+ no strict 'refs';
+ return map {
+ $_ => (ref($namespace->{$_}) eq 'SCALAR'
+ ? ($type_filter eq 'CODE' ? \&{$self->name . '::' . $_} : undef)
+ : *{$namespace->{$_}}{$type_filter})
+ } grep {
+ (ref($namespace->{$_})
+ ? (ref($namespace->{$_}) eq 'SCALAR' && $type_filter eq 'CODE')
+ : (ref(\$namespace->{$_}) eq 'GLOB'
+ && defined(*{$namespace->{$_}}{$type_filter})));
+ } keys %{$namespace};
+}
+
1;
__END__
By passing a C<$type_filter>, you can limit the list to only those
which match the filter (either SCALAR, ARRAY, HASH or CODE).
+=item B<get_all_package_symbols (?$type_filter)>
+
+Works exactly like C<list_all_package_symbols> but returns a HASH of
+name => thing mapping instead of just an ARRAY of names.
+
=back
=head1 AUTHORS
use strict;
use warnings;
-use Test::More tests => 88;
+use Test::More tests => 92;
use Test::Exception;
BEGIN {
ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
}
+# get_all_package_symbols
+
+{
+ my %syms = Foo->meta->get_all_package_symbols;
+
+ is_deeply(
+ [ sort keys %syms ],
+ [ sort Foo->meta->list_all_package_symbols ],
+ '... the fetched symbols are the same as the listed ones'
+ );
+}
+
+{
+ my %syms = Foo->meta->get_all_package_symbols('CODE');
+
+ is_deeply(
+ [ sort keys %syms ],
+ [ sort Foo->meta->list_all_package_symbols('CODE') ],
+ '... the fetched symbols are the same as the listed ones'
+ );
+
+ foreach my $symbol (keys %syms) {
+ is($syms{$symbol}, Foo->meta->get_package_symbol('&' . $symbol), '... got the right symbol');
+ }
+}
# check some errors