[Bast-commits] r5004 - in Devel-Declare/1.000/trunk:
lib/Devel/Declare/MethodInstaller t
rafl at dev.catalyst.perl.org
rafl at dev.catalyst.perl.org
Mon Oct 27 20:27:14 GMT 2008
Author: rafl
Date: 2008-10-27 20:27:13 +0000 (Mon, 27 Oct 2008)
New Revision: 5004
Modified:
Devel-Declare/1.000/trunk/lib/Devel/Declare/MethodInstaller/Simple.pm
Devel-Declare/1.000/trunk/t/methinstaller-simple.t
Log:
Refactor MethodInstaller::Simple.
It now has code_for() which the subclass can override to monkey with the magic
shadowed subroutine. This is handy if you want to employ Devel::BeginLift.
Modified: Devel-Declare/1.000/trunk/lib/Devel/Declare/MethodInstaller/Simple.pm
===================================================================
--- Devel-Declare/1.000/trunk/lib/Devel/Declare/MethodInstaller/Simple.pm 2008-10-27 17:27:40 UTC (rev 5003)
+++ Devel-Declare/1.000/trunk/lib/Devel/Declare/MethodInstaller/Simple.pm 2008-10-27 20:27:13 UTC (rev 5004)
@@ -67,6 +67,31 @@
return $attrs;
}
+sub code_for {
+ my ($self, $name) = @_;
+
+ if (defined $name) {
+ my $pkg = $self->get_curstash_name;
+ $name = join( '::', $pkg, $name )
+ unless( $name =~ /::/ );
+ return sub (&) {
+ my $code = shift;
+ # So caller() gets the subroutine name
+ no strict 'refs';
+ *{$name} = subname $name => $code;
+ return;
+ };
+ } else {
+ return sub (&) { shift };
+ }
+}
+
+sub install {
+ my ($self, $name ) = @_;
+
+ $self->shadow( $self->code_for($name) );
+}
+
sub parser {
my $self = shift;
$self->init(@_);
@@ -81,19 +106,10 @@
$inject = $self->scope_injector_call() . $inject;
}
$self->inject_if_block($inject, $attrs ? "sub ${attrs} " : '');
- if (defined $name) {
- my $pkg = $self->get_curstash_name;
- $name = join( '::', $pkg, $name )
- unless( $name =~ /::/ );
- $self->shadow( sub (&) {
- my $code = shift;
- # So caller() gets the subroutine name
- no strict 'refs';
- *{$name} = subname $name => $code;
- });
- } else {
- $self->shadow(sub (&) { shift });
- }
+
+ $self->install( $name );
+
+ return;
}
sub parse_proto { }
Modified: Devel-Declare/1.000/trunk/t/methinstaller-simple.t
===================================================================
--- Devel-Declare/1.000/trunk/t/methinstaller-simple.t 2008-10-27 17:27:40 UTC (rev 5003)
+++ Devel-Declare/1.000/trunk/t/methinstaller-simple.t 2008-10-27 20:27:13 UTC (rev 5004)
@@ -1,4 +1,15 @@
+#!/usr/bin/perl -w
+use strict;
+use Test::More 'no_plan';
+
+my $Have_Devel_BeginLift;
+BEGIN {
+ # setup_for_cv() introduced in 0.001001
+ $Have_Devel_BeginLift = eval q{ use Devel::BeginLift 0.001001; 1 };
+}
+
+
{
package MethodHandlers;
@@ -23,6 +34,17 @@
return $inject;
}
+ sub code_for {
+ my($self, $name) = @_;
+
+ my $code = $self->SUPER::code_for($name);
+
+ if( defined $name and $Have_Devel_BeginLift ) {
+ Devel::BeginLift->setup_for_cv($code);
+ }
+
+ return $code;
+ }
}
my ($test_method1, $test_method2, @test_list);
@@ -37,6 +59,13 @@
);
}
+ # Test at_BEGIN
+ SKIP: {
+ ::skip "Need Devel::BeginLift for compile time methods", 1
+ unless $Have_Devel_BeginLift;
+ ::can_ok( "DeclareTest", qw(new foo upgrade) );
+ }
+
method new {
my $class = ref $self || $self;
return bless({ @_ }, $class);
@@ -69,7 +98,6 @@
method leftie($left) : method { $self->{left} ||= $left; $self->{left} };
}
-use Test::More 'no_plan';
my $o = DeclareTest->new(attr => "value");
More information about the Bast-commits
mailing list