From: gfx Date: Fri, 26 Feb 2010 07:00:01 +0000 (+0900) Subject: Introduce install_subroutines() to reduce direct stash manipulation X-Git-Tag: 0.50_04~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1194aedef7b9a3f8c4a36fd7060c27b1a2907b87;p=gitmo%2FMouse.git Introduce install_subroutines() to reduce direct stash manipulation --- diff --git a/lib/Mouse/Exporter.pm b/lib/Mouse/Exporter.pm index 639b044..2ee752b 100644 --- a/lib/Mouse/Exporter.pm +++ b/lib/Mouse/Exporter.pm @@ -26,20 +26,19 @@ sub setup_import_methods{ my($import, $unimport) = $class->build_import_methods(%args); - no strict 'refs'; - - *{$exporting_package . '::import'} = $import; - *{$exporting_package . '::unimport'} = $unimport; - - # for backward compatibility - *{$exporting_package . '::export_to_level'} = sub{ - my($package, $level, undef, @args) = @_; # the third argument is redundant - $package->import({ into_level => $level + 1 }, @args); - }; - *{$exporting_package . '::export'} = sub{ - my($package, $into, @args) = @_; - $package->import({ into => $into }, @args); - }; + Mouse::Util::install_subroutines($exporting_package, + import => $import, + unimport => $unimport, + + export_to_level => sub { + my($package, $level, undef, @args) = @_; # the third argument is redundant + $package->import({ into_level => $level + 1 }, @args); + }, + export => sub { + my($package, $into, @args) = @_; + $package->import({ into => $into }, @args); + }, + ); return; } @@ -85,9 +84,9 @@ sub build_import_methods{ ($code_package, $code_name) = Mouse::Util::get_code_info($code); } else{ - no strict 'refs'; $code_package = $package; $code_name = $thingy; + no strict 'refs'; $code = \&{ $code_package . '::' . $code_name }; } @@ -194,18 +193,17 @@ sub do_import { } if(@exports){ + my @export_table; foreach my $keyword(@exports){ - no strict 'refs'; - *{$into.'::'.$keyword} = $spec->{EXPORTS}{$keyword} - || confess(qq{The $package package does not export "$keyword"}); + push @export_table, + $keyword => ($spec->{EXPORTS}{$keyword} + || confess(qq{The $package package does not export "$keyword"}) + ); } + Mouse::Util::install_subroutines($into, @export_table); } else{ - my $default = $spec->{DEFAULT}; - while(my($keyword, $code) = each %{$default}){ - no strict 'refs'; - *{$into.'::'.$keyword} = $code; - } + Mouse::Util::install_subroutines($into, %{$spec->{DEFAULT}}); } return; } diff --git a/lib/Mouse/Meta/Class.pm b/lib/Mouse/Meta/Class.pm index 607de86..7a9dd8b 100644 --- a/lib/Mouse/Meta/Class.pm +++ b/lib/Mouse/Meta/Class.pm @@ -362,10 +362,7 @@ sub _install_modifier { my $into = $self->name; $install_modifier->($into, $type, $name, $code); - $self->add_method($name => do{ - no strict 'refs'; - \&{ $into . '::' . $name }; - }); + $self->add_method($name => Mouse::Util::get_code_ref($into, $name)); return; }; } diff --git a/lib/Mouse/PurePerl.pm b/lib/Mouse/PurePerl.pm index 31e4a12..0003a9b 100644 --- a/lib/Mouse/PurePerl.pm +++ b/lib/Mouse/PurePerl.pm @@ -101,8 +101,7 @@ sub generate_isa_predicate_for { my $predicate = sub{ Scalar::Util::blessed($_[0]) && $_[0]->isa($for_class) }; if(defined $name){ - no strict 'refs'; - *{ caller() . '::' . $name } = $predicate; + Mouse::Util::install_subroutines(scalar caller, $name => $predicate); return; } @@ -128,8 +127,7 @@ sub generate_can_predicate_for { }; if(defined $name){ - no strict 'refs'; - *{ caller() . '::' . $name } = $predicate; + Mouse::Util::install_subroutines(scalar caller, $name => $predicate); return; } @@ -237,10 +235,9 @@ sub add_method { $self->{methods}->{$name} = $code; # Moose stores meta object here. - my $pkg = $self->name; - no strict 'refs'; - no warnings 'redefine', 'once'; - *{ $pkg . '::' . $name } = $code; + Mouse::Util::install_subroutines($self->name, + $name => $code, + ); return; } diff --git a/lib/Mouse/Util.pm b/lib/Mouse/Util.pm index 187aa34..b24cc31 100644 --- a/lib/Mouse/Util.pm +++ b/lib/Mouse/Util.pm @@ -3,6 +3,18 @@ use Mouse::Exporter; # enables strict and warnings sub get_linear_isa($;$); # must be here +sub install_subroutines { # must be here + my $into = shift; + + while(my($name, $code) = splice @_, 0, 2){ + no strict 'refs'; + no warnings 'once', 'redefine'; + use warnings FATAL => 'uninitialized'; + *{$into . '::' . $name} = \&{$code}; + } + return; +} + BEGIN{ # This is used in Mouse::PurePerl Mouse::Exporter->setup_import_methods(