From: Chia-liang Kao Date: Mon, 30 May 2011 08:20:55 +0000 (+0800) Subject: use the "redefine" warning flag when importing DD to determine if redefined subs... X-Git-Tag: 0.006005~1^2~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f1b89adc9d1738287606d2c7c5fcde06b1fcba53;p=p5sagit%2FDevel-Declare.git use the "redefine" warning flag when importing DD to determine if redefined subs should be warned --- diff --git a/lib/Devel/Declare/Context/Simple.pm b/lib/Devel/Declare/Context/Simple.pm index a7e3116..51a2d05 100644 --- a/lib/Devel/Declare/Context/Simple.pm +++ b/lib/Devel/Declare/Context/Simple.pm @@ -13,7 +13,7 @@ sub new { sub init { my $self = shift; - @{$self}{ qw(Declarator Offset) } = @_; + @{$self}{ qw(Declarator Offset WarningOnRedefined) } = @_; return $self; } @@ -32,6 +32,11 @@ sub declarator { return $self->{Declarator} } +sub warning_on_redefine { + my $self = shift; + return $self->{WarningOnRedefined} +} + sub skip_declarator { my $self = shift; my $decl = $self->declarator; diff --git a/lib/Devel/Declare/MethodInstaller/Simple.pm b/lib/Devel/Declare/MethodInstaller/Simple.pm index 6038310..cfe506e 100644 --- a/lib/Devel/Declare/MethodInstaller/Simple.pm +++ b/lib/Devel/Declare/MethodInstaller/Simple.pm @@ -17,10 +17,11 @@ sub install_methodhandler { *{$args{into}.'::'.$args{name}} = sub (&) {}; } + my $warnings = warnings::enabled("redefine"); my $ctx = $class->new(%args); Devel::Declare->setup_for( $args{into}, - { $args{name} => { const => sub { $ctx->parser(@_) } } } + { $args{name} => { const => sub { $ctx->parser(@_, $warnings) } } } ); } @@ -35,7 +36,11 @@ sub code_for { my $code = shift; # So caller() gets the subroutine name no strict 'refs'; - *{$name} = subname $name => $code; + my $installer = $self->warning_on_redefine + ? sub { *{$name} = subname $name => $code; } + : sub { no warnings 'redefine'; + *{$name} = subname $name => $code; }; + $installer->(); return; }; } else {