Merge branch 'stable'
[gitmo/Class-MOP.git] / lib / Class / MOP / Package.pm
index 61ce12d..e87df55 100644 (file)
@@ -4,23 +4,84 @@ package Class::MOP::Package;
 use strict;
 use warnings;
 
-use Scalar::Util 'blessed';
+use Scalar::Util 'blessed', 'reftype';
 use Carp         'confess';
+use Package::Stash;
 
-our $VERSION = '0.01';
+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, $package) = @_;
-    bless { '$:package' => $package } => $class;
+    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
+    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;
+    }
+}
+
+sub reinitialize {
+    my ( $class, @args ) = @_;
+
+    unshift @args, "package" if @args % 2;
+
+    my %options = @args;
+    my $package_name = delete $options{package};
+
+    (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";
+
+    $package_name = $package_name->name
+        if blessed $package_name;
+
+    Class::MOP::remove_metaclass_by_name($package_name);
+
+    $class->initialize($package_name, %options); # call with first arg form for compat
+}
+
+sub _new {
+    my $class = shift;
+
+    return Class::MOP::Class->initialize($class)->new_object(@_)
+        if $class ne __PACKAGE__;
+
+    my $params = @_ == 1 ? $_[0] : {@_};
+
+    return bless {
+        package   => $params->{package},
+
+        # 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,
+
+    } => $class;
 }
 
 # Attributes
@@ -29,102 +90,57 @@ sub initialize {
 # all these attribute readers will be bootstrapped 
 # away in the Class::MOP bootstrap section
 
-sub name { $_[0]->{'$:package'} }
+sub _package_stash {
+    $_[0]->{_package_stash} ||= Package::Stash->new($_[0]->name)
+}
+sub namespace {
+    $_[0]->_package_stash->namespace
+}
 
 # Class attributes
 
-my %SIGIL_MAP = (
-    '$' => 'SCALAR',
-    '@' => 'ARRAY',
-    '%' => 'HASH',
-    '&' => 'CODE',
-);
+# ... these functions have to touch the symbol table itself,.. yuk
 
 sub add_package_symbol {
-    my ($self, $variable, $initial_value) = @_;
-    
-    (defined $variable)
-        || confess "You must pass a variable name";    
-    
-    my ($sigil, $name) = ($variable =~ /^(.)(.*)$/); 
-    
-    (defined $sigil)
-        || confess "The variable name must include a sigil";    
-    
-    (exists $SIGIL_MAP{$sigil})
-        || confess "I do not recognize that sigil '$sigil'";
-    
-    no strict 'refs';
-    no warnings 'misc';
-    *{$self->name . '::' . $name} = $initial_value;    
+    my $self = shift;
+    $self->_package_stash->add_symbol(@_);
+}
+
+sub remove_package_glob {
+    my $self = shift;
+    $self->_package_stash->remove_glob(@_);
 }
 
+# ... these functions deal with stuff on the namespace level
+
 sub has_package_symbol {
-    my ($self, $variable) = @_;
-    (defined $variable)
-        || confess "You must pass a variable name";
-
-    my ($sigil, $name) = ($variable =~ /^(.)(.*)$/); 
-    
-    (defined $sigil)
-        || confess "The variable name must include a sigil";    
-    
-    (exists $SIGIL_MAP{$sigil})
-        || confess "I do not recognize that sigil '$sigil'";
-    
-    no strict 'refs';
-    defined *{$self->name . '::' . $name}{$SIGIL_MAP{$sigil}} ? 1 : 0;
-    
+    my $self = shift;
+    $self->_package_stash->has_symbol(@_);
 }
 
 sub get_package_symbol {
-    my ($self, $variable) = @_;    
-    (defined $variable)
-        || confess "You must pass a variable name";
-    
-    my ($sigil, $name) = ($variable =~ /^(.)(.*)$/); 
-    
-    (defined $sigil)
-        || confess "The variable name must include a sigil";    
-    
-    (exists $SIGIL_MAP{$sigil})
-        || confess "I do not recognize that sigil '$sigil'";
-    
-    no strict 'refs';
-    return *{$self->name . '::' . $name}{$SIGIL_MAP{$sigil}};
+    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, $variable) = @_;
-    
-    (defined $variable)
-        || confess "You must pass a variable name";
-        
-    my ($sigil, $name) = ($variable =~ /^(.)(.*)$/); 
-    
-    (defined $sigil)
-        || confess "The variable name must include a sigil";    
-    
-    (exists $SIGIL_MAP{$sigil})
-        || confess "I do not recognize that sigil '$sigil'"; 
-    
-    no strict 'refs';
-    if ($SIGIL_MAP{$sigil} eq 'SCALAR') {
-        undef ${$self->name . '::' . $name};    
-    }
-    elsif ($SIGIL_MAP{$sigil} eq 'ARRAY') {
-        undef @{$self->name . '::' . $name};    
-    }
-    elsif ($SIGIL_MAP{$sigil} eq 'HASH') {
-        undef %{$self->name . '::' . $name};    
-    }
-    elsif ($SIGIL_MAP{$sigil} eq 'CODE') {
-        undef &{$self->name . '::' . $name};    
-    }    
-    else {
-        confess "This should never ever ever happen";
-    }
+    my $self = shift;
+    $self->_package_stash->remove_symbol(@_);
+}
+
+sub list_all_package_symbols {
+    my $self = shift;
+    $self->_package_stash->list_all_symbols(@_);
+}
+
+sub get_all_package_symbols {
+    my $self = shift;
+    $self->_package_stash->get_all_symbols(@_);
 }
 
 1;
@@ -137,41 +153,108 @@ __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 >>
+
+This is returns the package's name, as passed to the constructor.
+
+=item B<< $metapackage->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<< $metapackage->add_package_symbol($variable_name, $initial_value) >>
+
+This method accepts a variable name and an optional initial value. The
+C<$variable_name> must contain a leading sigil.
+
+This method creates the variable in the package's symbol table, and
+sets it to the initial value if one was provided.
+
+=item B<< $metapackage->get_package_symbol($variable_name) >>
+
+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) >>
 
-=item B<initialize>
+This will list all the glob names associated with the current
+package. These names do not have leading sigils.
 
-=item B<name>
+You can provide an optional type filter, which should be one of
+'SCALAR', 'ARRAY', 'HASH', or 'CODE'.
 
-=item B<add_package_symbol>
+=item B<< $metapackage->get_all_package_symbols($type_filter) >>
 
-=item B<get_package_symbol>
+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<has_package_symbol>
+=item B<< Class::MOP::Package->meta >>
 
-=item B<remove_package_symbol>
+This will return a L<Class::MOP::Class> instance for this class.
 
 =back
 
-=head1 AUTHOR
+=head1 AUTHORS
 
 Stevan Little E<lt>stevan@iinteractive.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