0.58, no really this time
[gitmo/Class-MOP.git] / lib / Class / MOP / Package.pm
index 0e09c7f..dbac031 100644 (file)
@@ -7,18 +7,11 @@ use warnings;
 use Scalar::Util 'blessed';
 use Carp         'confess';
 
-our $VERSION   = '0.07';
+our $VERSION   = '0.09';
 our $AUTHORITY = 'cpan:STEVAN';
 
 use base 'Class::MOP::Object';
 
-# introspection
-
-sub meta { 
-    require Class::MOP::Class;
-    Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
-}
-
 # creation ...
 
 sub initialize {
@@ -46,7 +39,7 @@ sub initialize {
 # all these attribute readers will be bootstrapped 
 # away in the Class::MOP bootstrap section
 
-sub name      { $_[0]->{'$!package'}   }
+sub name      { $_[0]->{'$!package'} }
 sub namespace { 
     # NOTE:
     # because of issues with the Perl API 
@@ -56,7 +49,7 @@ sub namespace {
     # we could just store a ref and it would
     # Just Work, but oh well :\    
     no strict 'refs';    
-    \%{$_[0]->name . '::'} 
+    \%{$_[0]->{'$!package'} . '::'} 
 }
 
 # utility methods
@@ -96,9 +89,11 @@ sub add_package_symbol {
 
     my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable); 
 
+    my $pkg = $self->{'$!package'};
+
     no strict 'refs';
     no warnings 'redefine', 'misc';    
-    *{$self->name . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;      
+    *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;      
 }
 
 sub remove_package_glob {
@@ -114,7 +109,9 @@ sub has_package_symbol {
 
     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 
@@ -125,15 +122,15 @@ sub has_package_symbol {
     # 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;
     }
 }
 
@@ -142,10 +139,12 @@ sub get_package_symbol {
 
     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};
@@ -155,7 +154,7 @@ sub get_package_symbol {
         }
     }
     else {
-        return *{$self->namespace->{$name}}{$type};
+        return *{$namespace->{$name}}{$type};
     }
 }
 
@@ -203,11 +202,13 @@ sub remove_package_symbol {
 
 sub list_all_package_symbols {
     my ($self, $type_filter) = @_;
-    return keys %{$self->namespace} unless defined $type_filter;
+
+    my $namespace = $self->namespace;
+    return keys %{$namespace} unless defined $type_filter;
+    
     # NOTE:
     # or we can filter based on 
     # type (SCALAR|ARRAY|HASH|CODE)
-    my $namespace = $self->namespace;
     return grep { 
         (ref($namespace->{$_})
             ? (ref($namespace->{$_}) eq 'SCALAR' && $type_filter eq 'CODE')
@@ -216,6 +217,27 @@ sub list_all_package_symbols {
     } keys %{$namespace};
 }
 
+sub get_all_package_symbols {
+    my ($self, $type_filter) = @_;
+    my $namespace = $self->namespace;
+    return %{$namespace} unless defined $type_filter;
+    
+    # NOTE:
+    # or we can filter based on 
+    # type (SCALAR|ARRAY|HASH|CODE)
+    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__
@@ -291,6 +313,11 @@ the package.
 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