X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FModule.pm;h=be07d803840ff18aaca757632744a7a58c0f77a6;hb=902174eb6dc2afbe8a2b8fb0a27446b3276787d8;hp=7d2a1e9fbf1bab1223da156c4902807065a7f747;hpb=637d4f17e6965551c032a3115dcbbb4d8e01fbf5;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Module.pm b/lib/Mouse/Meta/Module.pm index 7d2a1e9..be07d80 100755 --- a/lib/Mouse/Meta/Module.pm +++ b/lib/Mouse/Meta/Module.pm @@ -15,7 +15,7 @@ if(Mouse::Util::MOUSE_XS){ } sub _metaclass_cache { # DEPRECATED - my($class, $name) = @_; + my($self, $name) = @_; Carp::cluck('_metaclass_cache() has been deprecated. Use Mouse::Util::get_metaclass_by_name() instead'); return $METAS{$name}; } @@ -38,6 +38,9 @@ sub reinitialize { ($package_name && !ref($package_name)) || $class->throw_error("You must pass a package name and it cannot be blessed"); + if(exists $METAS{$package_name}) { + unshift @args, %{ $METAS{$package_name} }; + } delete $METAS{$package_name}; return $class->initialize($package_name, @args); } @@ -63,19 +66,6 @@ sub name; sub namespace; -# The followings are Class::MOP specific methods - -#sub version { no strict 'refs'; ${shift->name.'::VERSION'} } -#sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} } -#sub identifier { -# my $self = shift; -# return join '-' => ( -# $self->name, -# ($self->version || ()), -# ($self->authority || ()), -# ); -#} - # add_attribute is an abstract method sub get_attribute_map { # DEPRECATED @@ -163,7 +153,8 @@ sub _collect_methods { # Mouse specific my $subname = ( caller(1) )[3]; $meta->throw_error( sprintf( - 'Methods passed to %s must be provided as a list, ArrayRef or regular expression, not %s', + 'Methods passed to %s must be provided as a list,' + . ' ArrayRef or regular expression, not %s', $subname, $type, ) @@ -177,147 +168,149 @@ sub _collect_methods { # Mouse specific return @methods; } +my $ANON_SERIAL = 0; # anonymous class/role id +my %IMMORTALS; # immortal anonymous classes -{ - my $ANON_SERIAL = 0; - - my %IMMORTALS; - - sub create { - my($self, $package_name, %options) = @_; +sub create { + my($self, $package_name, %options) = @_; - my $class = ref($self) || $self; - $self->throw_error('You must pass a package name') if @_ < 2; - - my $superclasses; - if(exists $options{superclasses}){ - if(Mouse::Util::is_a_metarole($self)){ - delete $options{superclasses}; - } - else{ - $superclasses = delete $options{superclasses}; - (ref $superclasses eq 'ARRAY') - || $self->throw_error("You must pass an ARRAY ref of superclasses"); - } - } + my $class = ref($self) || $self; + $self->throw_error('You must pass a package name') if @_ < 2; - my $attributes = delete $options{attributes}; - if(defined $attributes){ - (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH') - || $self->throw_error("You must pass an ARRAY ref of attributes"); + my $superclasses; + if(exists $options{superclasses}){ + if(Mouse::Util::is_a_metarole($self)){ + delete $options{superclasses}; } - my $methods = delete $options{methods}; - if(defined $methods){ - (ref $methods eq 'HASH') - || $self->throw_error("You must pass a HASH ref of methods"); - } - my $roles = delete $options{roles}; - if(defined $roles){ - (ref $roles eq 'ARRAY') - || $self->throw_error("You must pass an ARRAY ref of roles"); - } - my $mortal; - my $cache_key; - - if(!defined $package_name){ # anonymous - $mortal = !$options{cache}; - - # anonymous but immortal - if(!$mortal){ - # something like Super::Class|Super::Class::2=Role|Role::1 - $cache_key = join '=' => ( - join('|', @{$superclasses || []}), - join('|', sort @{$roles || []}), - ); - return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key}; - } - $options{anon_serial_id} = ++$ANON_SERIAL; - $package_name = $class . '::__ANON__::' . $ANON_SERIAL; + else{ + $superclasses = delete $options{superclasses}; + (ref $superclasses eq 'ARRAY') + || $self->throw_error("You must pass an ARRAY ref of superclasses"); } + } - # instantiate a module - { - no strict 'refs'; - ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version}; - ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority}; + my $attributes = delete $options{attributes}; + if(defined $attributes){ + (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH') + || $self->throw_error("You must pass an ARRAY ref of attributes"); + } + my $methods = delete $options{methods}; + if(defined $methods){ + (ref $methods eq 'HASH') + || $self->throw_error("You must pass a HASH ref of methods"); + } + my $roles = delete $options{roles}; + if(defined $roles){ + (ref $roles eq 'ARRAY') + || $self->throw_error("You must pass an ARRAY ref of roles"); + } + my $mortal; + my $cache_key; + + if(!defined $package_name){ # anonymous + $mortal = !$options{cache}; + + # anonymous but immortal + if(!$mortal){ + # something like Super::Class|Super::Class::2=Role|Role::1 + $cache_key = join '=' => ( + join('|', @{$superclasses || []}), + join('|', sort @{$roles || []}), + ); + return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key}; } + $options{anon_serial_id} = ++$ANON_SERIAL; + $package_name = $class . '::__ANON__::' . $ANON_SERIAL; + } - my $meta = $self->initialize( $package_name, %options); - - Scalar::Util::weaken $METAS{$package_name} - if $mortal; - - $meta->add_method(meta => sub{ - $self->initialize(ref($_[0]) || $_[0]); - }); - - $meta->superclasses(@{$superclasses}) - if defined $superclasses; - - # NOTE: - # process attributes first, so that they can - # install accessors, but locally defined methods - # can then overwrite them. It is maybe a little odd, but - # I think this should be the order of things. - if (defined $attributes) { - if(ref($attributes) eq 'ARRAY'){ - # array of Mouse::Meta::Attribute - foreach my $attr (@{$attributes}) { - $meta->add_attribute($attr); - } - } - else{ - # hash map of name and attribute spec pairs - while(my($name, $attr) = each %{$attributes}){ - $meta->add_attribute($name => $attr); - } + # instantiate a module + { + no strict 'refs'; + ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version}; + ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority}; + } + + my $meta = $self->initialize( $package_name, %options); + + Scalar::Util::weaken $METAS{$package_name} + if $mortal; + + $meta->add_method(meta => sub { + $self->initialize(ref($_[0]) || $_[0]); + }); + + $meta->superclasses(@{$superclasses}) + if defined $superclasses; + + # NOTE: + # process attributes first, so that they can + # install accessors, but locally defined methods + # can then overwrite them. It is maybe a little odd, but + # I think this should be the order of things. + if (defined $attributes) { + if(ref($attributes) eq 'ARRAY'){ + # array of Mouse::Meta::Attribute + foreach my $attr (@{$attributes}) { + $meta->add_attribute($attr); } } - if (defined $methods) { - while(my($method_name, $method_body) = each %{$methods}){ - $meta->add_method($method_name, $method_body); + else{ + # hash map of name and attribute spec pairs + while(my($name, $attr) = each %{$attributes}){ + $meta->add_attribute($name => $attr); } } - if (defined $roles){ - Mouse::Util::apply_all_roles($package_name, @{$roles}); - } - - if($cache_key){ - $IMMORTALS{$cache_key} = $meta; + } + if (defined $methods) { + while(my($method_name, $method_body) = each %{$methods}){ + $meta->add_method($method_name, $method_body); } + } + if (defined $roles){ + Mouse::Util::apply_all_roles($package_name, @{$roles}); + } - return $meta; + if($cache_key){ + $IMMORTALS{$cache_key} = $meta; } - sub DESTROY{ - my($self) = @_; + return $meta; +} - return if $Mouse::Util::in_global_destruction; +sub DESTROY{ + my($self) = @_; - my $serial_id = $self->{anon_serial_id}; + return if $Mouse::Util::in_global_destruction; - return if !$serial_id; + my $serial_id = $self->{anon_serial_id}; + return if !$serial_id; - # @ISA is a magical variable, so we clear it manually. - @{$self->{superclasses}} = () if exists $self->{superclasses}; + # XXX: cleaning stash with threads causes panic/SEGV on legacy perls. + if(exists $INC{'threads.pm'}) { + # (caller)[2] indicates the caller's line number, + # which is zero when the current thread is joining (destroying). + return if( (caller)[2] == 0); + } - # Then, clear the symbol table hash - %{$self->namespace} = (); + # clean up mortal anonymous class stuff - my $name = $self->name; - delete $METAS{$name}; + # @ISA is a magical variable, so we must clear it manually. + @{$self->{superclasses}} = () if exists $self->{superclasses}; - $name =~ s/ $serial_id \z//xms; + # Then, clear the symbol table hash + %{$self->namespace} = (); - no strict 'refs'; - delete ${$name}{ $serial_id . '::' }; + my $name = $self->name; + delete $METAS{$name}; - return; - } + $name =~ s/ $serial_id \z//xms; + no strict 'refs'; + delete ${$name}{ $serial_id . '::' }; + return; } sub throw_error{ - my($class, $message, %args) = @_; + my($self, $message, %args) = @_; local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0); local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though @@ -335,11 +328,15 @@ __END__ =head1 NAME -Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role +Mouse::Meta::Module - The common base class of Mouse::Meta::Class and Mouse::Meta::Role =head1 VERSION -This document describes Mouse version 0.50_03 +This document describes Mouse version 0.70 + +=head1 DESCRIPTION + +This class is an abstract base class of meta classes and meta roles. =head1 SEE ALSO