From: Yuval Kogman Date: Thu, 14 Aug 2008 18:21:30 +0000 (+0000) Subject: register on initialize, and move reinitialize to Package X-Git-Tag: 0_64_01~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a19fcb5bdd84da01f63cfacd15fbf7a0f4ea2430;p=gitmo%2FClass-MOP.git register on initialize, and move reinitialize to Package --- diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index e8cf77f..83088da 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -37,15 +37,6 @@ sub initialize { || $class->construct_class_instance(package => $package_name, @_); } -sub reinitialize { - my $class = shift; - my $package_name = shift; - (defined $package_name && $package_name && !blessed($package_name)) - || confess "You must pass a package name and it cannot be blessed"; - Class::MOP::remove_metaclass_by_name($package_name); - $class->construct_class_instance('package' => $package_name, @_); -} - # NOTE: (meta-circularity) # this is a special form of &construct_instance # (see below), which is used to construct class @@ -1217,12 +1208,6 @@ as we use a special reserved slot (C<__MOP__>) to store this. This initializes and returns returns a B object for a given a C<$package_name>. -=item B - -This removes the old metaclass, and creates a new one in it's place. -Do B use this unless you really know what you are doing, it could -very easily make a very large mess of your program. - =item B This will construct an instance of B, it is diff --git a/lib/Class/MOP/Package.pm b/lib/Class/MOP/Package.pm index b54dbc0..e5c270b 100644 --- a/lib/Class/MOP/Package.pm +++ b/lib/Class/MOP/Package.pm @@ -19,10 +19,26 @@ sub initialize { my $package_name = shift; # we hand-construct the class # until we can bootstrap it - return Class::MOP::get_metaclass_by_name($package_name) || - $class->_new({ - 'package' => $package_name, - }); + if ( my $meta = Class::MOP::get_metaclass_by_name($package_name) ) { + return $meta; + } else { + my $meta = $class->_new({ + 'package' => $package_name, + }); + + Class::MOP::store_metaclass_by_name($package_name, $meta); + + return $meta; + } +} + +sub reinitialize { + my $class = shift; + my $package_name = shift; + (defined $package_name && $package_name && !blessed($package_name)) + || confess "You must pass a package name and it cannot be blessed"; + Class::MOP::remove_metaclass_by_name($package_name); + $class->initialize('package' => $package_name, @_); } sub _new { @@ -299,6 +315,12 @@ Returns a metaclass for this package. This will initialize a Class::MOP::Package instance which represents the package of C<$package_name>. +=item B + +This removes the old metaclass, and creates a new one in it's place. +Do B use this unless you really know what you are doing, it could +very easily make a very large mess of your program. + =item B This is a read-only attribute which returns the package name for the diff --git a/t/010_self_introspection.t b/t/010_self_introspection.t index 11d96a1..9eee7af 100644 --- a/t/010_self_introspection.t +++ b/t/010_self_introspection.t @@ -30,7 +30,7 @@ isa_ok($class_mop_module_meta, 'Class::MOP::Module'); my @class_mop_package_methods = qw( _new - initialize + initialize reinitialize name namespace @@ -50,7 +50,7 @@ my @class_mop_module_methods = qw( my @class_mop_class_methods = qw( _new - initialize reinitialize create + initialize create update_package_cache_flag reset_package_cache_flag