3 package MethodHandlers;
7 use base 'Devel::Declare::MethodInstaller::Simple';
9 # undef -> my ($self) = shift;
10 # '' -> my ($self) = @_;
11 # '$foo' -> my ($self, $foo) = @_;
16 my $inject = 'my ($self';
18 $inject .= ", $proto" if length($proto);
19 $inject .= ') = @_; ';
21 $inject .= ') = shift;';
28 my ($test_method1, $test_method2, @test_list);
33 BEGIN { # normally, this'd go in MethodHandlers::import
34 MethodHandlers->install_methodhandler(
41 my $class = ref $self || $self;
42 return bless({ @_ }, $class);
46 return (ref $self).': Foo: '.$foo;
49 method upgrade(){ # no spaces to make case pathological
50 bless($self, 'DeclareTest2');
53 method DeclareTest2::bar () {
54 return 'DeclareTest2: bar';
57 $test_method1 = method {
58 return join(', ', $self->{attr}, $_[1]);
61 $test_method2 = method ($what) {
62 return join(', ', ref $self, $what);
65 method main () { return "main"; }
67 @test_list = (method { 1 }, sub { 2 }, method () { 3 }, sub { 4 });
69 method leftie($left) : method { $self->{left} ||= $left; $self->{left} };
72 use Test::More 'no_plan';
74 my $o = DeclareTest->new(attr => "value");
76 isa_ok($o, 'DeclareTest');
78 is($o->{attr}, 'value', '@_ args ok');
80 is($o->foo('yay'), 'DeclareTest: Foo: yay', 'method with argument ok');
82 is($o->main, 'main', 'declaration of package named method ok');
84 $o->leftie( 'attributes work' );
85 is($o->leftie, 'attributes work', 'code attributes intact');
89 isa_ok($o, 'DeclareTest2');
91 is($o->bar, 'DeclareTest2: bar', 'absolute method declaration ok');
93 is($o->$test_method1('no', 'yes'), 'value, yes', 'anon method with @_ ok');
95 is($o->$test_method2('this'), 'DeclareTest2, this', 'anon method with proto ok');
97 is_deeply([ map { $_->() } @test_list ], [ 1, 2, 3, 4], 'binding ok');