5 Devel::Declare->install_declarator(
6 'DeclareTest', 'method', DECLARE_PACKAGE | DECLARE_PROTO,
8 my ($name, $proto) = @_;
9 return 'my $self = shift;' unless defined $proto && $proto ne '@_';
10 return 'my ($self'.(length $proto ? ", ${proto}" : "").') = @_;';
13 my ($name, $proto, $sub) = @_;
14 if (defined $name && length $name) {
15 unless ($name =~ /::/) {
16 $name = "DeclareTest::${name}";
27 my ($test_method1, $test_method2);
33 my $class = ref $self || $self;
34 return bless({ @_ }, $class);
38 return (ref $self).': Foo: '.$foo;
42 bless($self, 'DeclareTest2');
45 method DeclareTest2::bar () {
46 return 'DeclareTest2: bar';
49 $test_method1 = method (@_) {
50 return join(', ', $self->{attr}, $_[1]);
53 $test_method2 = method ($what) {
54 return join(', ', ref $self, $what);
59 use Test::More 'no_plan';
61 my $o = DeclareTest->new(attr => "value");
63 isa_ok($o, 'DeclareTest');
65 is($o->{attr}, 'value', '@_ args ok');
67 is($o->foo('yay'), 'DeclareTest: Foo: yay', 'method with argument ok');
71 isa_ok($o, 'DeclareTest2');
73 is($o->bar, 'DeclareTest2: bar', 'absolute method declaration ok');
75 is($o->$test_method1('no', 'yes'), 'value, yes', 'anon method with @_ ok');
77 is($o->$test_method2('this'), 'DeclareTest2, this', 'anon method with proto ok');