5 my $Have_Devel_BeginLift;
7 # setup_for_cv() introduced in 0.001001
8 $Have_Devel_BeginLift = eval q{ use Devel::BeginLift 0.001001; 1 };
13 package MethodHandlers;
17 use base 'Devel::Declare::MethodInstaller::Simple';
19 # undef -> my ($self) = shift;
20 # '' -> my ($self) = @_;
21 # '$foo' -> my ($self, $foo) = @_;
26 my $inject = 'my ($self';
28 $inject .= ", $proto" if length($proto);
29 $inject .= ') = @_; ';
31 $inject .= ') = shift;';
37 my($self, $name) = @_;
39 my $code = $self->SUPER::code_for($name);
41 if( defined $name and $Have_Devel_BeginLift ) {
42 Devel::BeginLift->setup_for_cv($code);
49 my ($test_method1, $test_method2, @test_list);
54 BEGIN { # normally, this'd go in MethodHandlers::import
55 MethodHandlers->install_methodhandler(
63 ::skip "Need Devel::BeginLift for compile time methods", 1
64 unless $Have_Devel_BeginLift;
65 ::can_ok( "DeclareTest", qw(new foo upgrade) );
69 my $class = ref $self || $self;
70 return bless({ @_ }, $class);
74 return (ref $self).': Foo: '.$foo;
77 method upgrade(){ # no spaces to make case pathological
78 bless($self, 'DeclareTest2');
81 method DeclareTest2::bar () {
82 return 'DeclareTest2: bar';
85 $test_method1 = method {
86 return join(', ', $self->{attr}, $_[1]);
89 $test_method2 = method ($what) {
90 return join(', ', ref $self, $what);
93 method main () { return "main"; }
95 @test_list = (method { 1 }, sub { 2 }, method () { 3 }, sub { 4 });
97 method leftie($left) : method { $self->{left} ||= $left; $self->{left} };
101 my $o = DeclareTest->new(attr => "value");
103 isa_ok($o, 'DeclareTest');
105 is($o->{attr}, 'value', '@_ args ok');
107 is($o->foo('yay'), 'DeclareTest: Foo: yay', 'method with argument ok');
109 is($o->main, 'main', 'declaration of package named method ok');
111 $o->leftie( 'attributes work' );
112 is($o->leftie, 'attributes work', 'code attributes intact');
116 isa_ok($o, 'DeclareTest2');
118 is($o->bar, 'DeclareTest2: bar', 'absolute method declaration ok');
120 is($o->$test_method1('no', 'yes'), 'value, yes', 'anon method with @_ ok');
122 is($o->$test_method2('this'), 'DeclareTest2, this', 'anon method with proto ok');
124 is_deeply([ map { $_->() } @test_list ], [ 1, 2, 3, 4], 'binding ok');