Merge branch 'stable'
[gitmo/Class-MOP.git] / lib / Class / MOP / Package.pm
index 87b4216..e87df55 100644 (file)
@@ -4,140 +4,143 @@ package Class::MOP::Package;
 use strict;
 use warnings;
 
-use Scalar::Util 'blessed';
+use Scalar::Util 'blessed', 'reftype';
 use Carp         'confess';
-use Symbol       'gensym';
+use Package::Stash;
 
-our $VERSION = '0.02';
+our $VERSION   = '1.12';
+$VERSION = eval $VERSION;
+our $AUTHORITY = 'cpan:STEVAN';
 
-# introspection
-
-sub meta { 
-    require Class::MOP::Class;
-    Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
-}
+use base 'Class::MOP::Object';
 
 # creation ...
 
 sub initialize {
-    my $class        = shift;
-    my $package_name = shift;
+    my ( $class, @args ) = @_;
+
+    unshift @args, "package" if @args % 2;
+
+    my %options = @args;
+    my $package_name = $options{package};
+
+
     # we hand-construct the class 
     # until we can bootstrap it
-    no strict 'refs';
-    return bless { 
-        '$:package'   => $package_name,
-        '%:namespace' => \%{$package_name . '::'},
-    } => $class;
+    if ( my $meta = Class::MOP::get_metaclass_by_name($package_name) ) {
+        return $meta;
+    } else {
+        my $meta = ( ref $class || $class )->_new({
+            'package'   => $package_name,
+            %options,
+        });
+        Class::MOP::store_metaclass_by_name($package_name, $meta);
+
+        return $meta;
+    }
 }
 
-# Attributes
+sub reinitialize {
+    my ( $class, @args ) = @_;
 
-# NOTE:
-# all these attribute readers will be bootstrapped 
-# away in the Class::MOP bootstrap section
+    unshift @args, "package" if @args % 2;
 
-sub name      { $_[0]->{'$:package'}   }
-sub namespace { $_[0]->{'%:namespace'} }
+    my %options = @args;
+    my $package_name = delete $options{package};
 
-# utility methods
+    (defined $package_name && $package_name
+      && (!blessed $package_name || $package_name->isa('Class::MOP::Package')))
+        || confess "You must pass a package name or an existing Class::MOP::Package instance";
 
-{
-    my %SIGIL_MAP = (
-        '$' => 'SCALAR',
-        '@' => 'ARRAY',
-        '%' => 'HASH',
-        '&' => 'CODE',
-    );
-    
-    sub _deconstruct_variable_name {
-        my ($self, $variable) = @_;
+    $package_name = $package_name->name
+        if blessed $package_name;
 
-        (defined $variable)
-            || confess "You must pass a variable name";    
+    Class::MOP::remove_metaclass_by_name($package_name);
 
-        my ($sigil, $name) = ($variable =~ /^(.)(.*)$/); 
+    $class->initialize($package_name, %options); # call with first arg form for compat
+}
 
-        (defined $sigil)
-            || confess "The variable name must include a sigil";    
+sub _new {
+    my $class = shift;
 
-        (exists $SIGIL_MAP{$sigil})
-            || confess "I do not recognize that sigil '$sigil'";    
-        
-        return ($name, $sigil, $SIGIL_MAP{$sigil});
-    }
-}
+    return Class::MOP::Class->initialize($class)->new_object(@_)
+        if $class ne __PACKAGE__;
 
-# Class attributes
+    my $params = @_ == 1 ? $_[0] : {@_};
 
-sub add_package_symbol {
-    my ($self, $variable, $initial_value) = @_;
+    return bless {
+        package   => $params->{package},
 
-    my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable); 
+        # NOTE:
+        # because of issues with the Perl API
+        # to the typeglob in some versions, we
+        # need to just always grab a new
+        # reference to the hash in the accessor.
+        # Ideally we could just store a ref and
+        # it would Just Work, but oh well :\
 
+        namespace => \undef,
 
-    no strict 'refs';
-    no warnings 'redefine', 'misc';
-    *{$self->name . '::' . $name} = $initial_value;    
+    } => $class;
 }
 
-sub has_package_symbol {
-    my ($self, $variable) = @_;
+# Attributes
 
-    my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable); 
+# NOTE:
+# all these attribute readers will be bootstrapped 
+# away in the Class::MOP bootstrap section
 
-    return 0 unless exists $self->namespace->{$name};   
-    defined *{$self->namespace->{$name}}{$type} ? 1 : 0;
+sub _package_stash {
+    $_[0]->{_package_stash} ||= Package::Stash->new($_[0]->name)
+}
+sub namespace {
+    $_[0]->_package_stash->namespace
 }
 
-sub get_package_symbol {
-    my ($self, $variable) = @_;    
+# Class attributes
 
-    my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable); 
+# ... these functions have to touch the symbol table itself,.. yuk
 
-    $self->add_package_symbol($variable)
-        unless exists $self->namespace->{$name};
-    return *{$self->namespace->{$name}}{$type};
+sub add_package_symbol {
+    my $self = shift;
+    $self->_package_stash->add_symbol(@_);
 }
 
-sub remove_package_symbol {
-    my ($self, $variable) = @_;
+sub remove_package_glob {
+    my $self = shift;
+    $self->_package_stash->remove_glob(@_);
+}
 
-    my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable); 
+# ... these functions deal with stuff on the namespace level
 
-    no strict 'refs';
-    if ($type eq 'SCALAR') {
-        undef ${$self->name . '::' . $name};    
-    }
-    elsif ($type eq 'ARRAY') {
-        undef @{$self->name . '::' . $name};    
-    }
-    elsif ($type eq 'HASH') {
-        undef %{$self->name . '::' . $name};    
-    }
-    elsif ($type eq 'CODE') {
-        # FIXME:
-        # this is crap, it is probably much 
-        # easier to write this in XS.
-        my ($scalar, @array, %hash);
-        $scalar = ${$self->name . '::' . $name} if defined *{$self->namespace->{$name}}{SCALAR};
-        @array  = @{$self->name . '::' . $name} if defined *{$self->namespace->{$name}}{ARRAY};
-        %hash   = %{$self->name . '::' . $name} if defined *{$self->namespace->{$name}}{HASH};
-        
-        delete ${$self->name . '::'}{$name};
-        
-        ${$self->name . '::' . $name} = $scalar if defined $scalar;
-        @{$self->name . '::' . $name} = @array  if scalar  @array;
-        %{$self->name . '::' . $name} = %hash   if keys    %hash;            
-    }    
-    else {
-        confess "This should never ever ever happen";
-    }
+sub has_package_symbol {
+    my $self = shift;
+    $self->_package_stash->has_symbol(@_);
+}
+
+sub get_package_symbol {
+    my $self = shift;
+    $self->_package_stash->get_symbol(@_);
+}
+
+sub get_or_add_package_symbol {
+    my $self = shift;
+    $self->_package_stash->get_or_add_symbol(@_);
+}
+
+sub remove_package_symbol {
+    my $self = shift;
+    $self->_package_stash->remove_symbol(@_);
 }
 
 sub list_all_package_symbols {
-    my ($self) = @_;
-    return keys %{$self->namespace};
+    my $self = shift;
+    $self->_package_stash->list_all_symbols(@_);
+}
+
+sub get_all_package_symbols {
+    my $self = shift;
+    $self->_package_stash->get_all_symbols(@_);
 }
 
 1;
@@ -150,31 +153,94 @@ __END__
 
 Class::MOP::Package - Package Meta Object
 
-=head1 SYNOPSIS
-
 =head1 DESCRIPTION
 
+The Package Protocol provides an abstraction of a Perl 5 package. A
+package is basically namespace, and this module provides methods for
+looking at and changing that namespace's symbol table.
+
 =head1 METHODS
 
 =over 4
 
-=item B<meta>
+=item B<< Class::MOP::Package->initialize($package_name) >>
+
+This method creates a new C<Class::MOP::Package> instance which
+represents specified package. If an existing metaclass object exists
+for the package, that will be returned instead.
+
+=item B<< Class::MOP::Package->reinitialize($package) >>
+
+This method forcibly removes any existing metaclass for the package
+before calling C<initialize>. In contrast to C<initialize>, you may
+also pass an existing C<Class::MOP::Package> instance instead of just
+a package name as C<$package>.
+
+Do not call this unless you know what you are doing.
+
+=item B<< $metapackage->name >>
 
-=item B<initialize>
+This is returns the package's name, as passed to the constructor.
 
-=item B<name>
+=item B<< $metapackage->namespace >>
 
-=item B<namespace>
+This returns a hash reference to the package's symbol table. The keys
+are symbol names and the values are typeglob references.
 
-=item B<add_package_symbol>
+=item B<< $metapackage->add_package_symbol($variable_name, $initial_value) >>
 
-=item B<get_package_symbol>
+This method accepts a variable name and an optional initial value. The
+C<$variable_name> must contain a leading sigil.
 
-=item B<has_package_symbol>
+This method creates the variable in the package's symbol table, and
+sets it to the initial value if one was provided.
 
-=item B<remove_package_symbol>
+=item B<< $metapackage->get_package_symbol($variable_name) >>
 
-=item B<list_all_package_symbols>
+Given a variable name, this method returns the variable as a reference
+or undef if it does not exist. The C<$variable_name> must contain a
+leading sigil.
+
+=item B<< $metapackage->get_or_add_package_symbol($variable_name) >>
+
+Given a variable name, this method returns the variable as a reference.
+If it does not exist, a default value will be generated if possible. The
+C<$variable_name> must contain a leading sigil.
+
+=item B<< $metapackage->has_package_symbol($variable_name) >>
+
+Returns true if there is a package variable defined for
+C<$variable_name>. The C<$variable_name> must contain a leading sigil.
+
+=item B<< $metapackage->remove_package_symbol($variable_name) >>
+
+This will remove the package variable specified C<$variable_name>. The
+C<$variable_name> must contain a leading sigil.
+
+=item B<< $metapackage->remove_package_glob($glob_name) >>
+
+Given the name of a glob, this will remove that glob from the
+package's symbol table. Glob names do not include a sigil. Removing
+the glob removes all variables and subroutines with the specified
+name.
+
+=item B<< $metapackage->list_all_package_symbols($type_filter) >>
+
+This will list all the glob names associated with the current
+package. These names do not have leading sigils.
+
+You can provide an optional type filter, which should be one of
+'SCALAR', 'ARRAY', 'HASH', or 'CODE'.
+
+=item B<< $metapackage->get_all_package_symbols($type_filter) >>
+
+This works much like C<list_all_package_symbols>, but it returns a
+hash reference. The keys are glob names and the values are references
+to the value for that name.
+
+=item B<< Class::MOP::Package->meta >>
+
+This will return a L<Class::MOP::Class> instance for this class.
 
 =back
 
@@ -182,15 +248,13 @@ Class::MOP::Package - Package Meta Object
 
 Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
-Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
-
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006 by Infinity Interactive, Inc.
+Copyright 2006-2010 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>
 
 This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself.
 
-=cut
\ No newline at end of file
+=cut