1f2a19305Safresh1#!./perl 2f2a19305Safresh1 3f2a19305Safresh1BEGIN { 4f2a19305Safresh1 chdir 't' if -d 't'; 5f2a19305Safresh1 require './test.pl'; 6f2a19305Safresh1 set_up_inc('../lib'); 7f2a19305Safresh1 require Config; 8f2a19305Safresh1} 9f2a19305Safresh1 10f2a19305Safresh1use v5.36; 11f2a19305Safresh1use feature 'class'; 12f2a19305Safresh1no warnings 'experimental::class'; 13f2a19305Safresh1 14f2a19305Safresh1# $self in method 15f2a19305Safresh1{ 16*5486feefSafresh1 class Testcase1 { 17f2a19305Safresh1 method retself { return $self } 18f2a19305Safresh1 } 19f2a19305Safresh1 20*5486feefSafresh1 my $obj = Testcase1->new; 21f2a19305Safresh1 is($obj->retself, $obj, '$self inside method'); 22f2a19305Safresh1} 23f2a19305Safresh1 24f2a19305Safresh1# methods have signatures; signatures do not capture $self 25f2a19305Safresh1{ 26f2a19305Safresh1 # Turn off the 'signatures' feature to prove that 'method' is always 27f2a19305Safresh1 # signatured even without it 28f2a19305Safresh1 no feature 'signatures'; 29f2a19305Safresh1 30*5486feefSafresh1 class Testcase2 { 31f2a19305Safresh1 method retfirst ( $x = 123 ) { return $x; } 32f2a19305Safresh1 } 33f2a19305Safresh1 34*5486feefSafresh1 my $obj = Testcase2->new; 35f2a19305Safresh1 is($obj->retfirst, 123, 'method signature params work'); 36f2a19305Safresh1 is($obj->retfirst(456), 456, 'method signature params skip $self'); 37f2a19305Safresh1} 38f2a19305Safresh1 39f2a19305Safresh1# methods can still capture regular package lexicals 40f2a19305Safresh1{ 41*5486feefSafresh1 class Testcase3 { 42f2a19305Safresh1 my $count; 43f2a19305Safresh1 method inc { return $count++ } 44f2a19305Safresh1 } 45f2a19305Safresh1 46*5486feefSafresh1 my $obj1 = Testcase3->new; 47f2a19305Safresh1 $obj1->inc; 48f2a19305Safresh1 49f2a19305Safresh1 is($obj1->inc, 1, '$obj1->inc sees 1'); 50f2a19305Safresh1 51*5486feefSafresh1 my $obj2 = Testcase3->new; 52f2a19305Safresh1 is($obj2->inc, 2, '$obj2->inc sees 2'); 53f2a19305Safresh1} 54f2a19305Safresh1 55f2a19305Safresh1# $self is shifted from @_ 56f2a19305Safresh1{ 57*5486feefSafresh1 class Testcase4 { 58f2a19305Safresh1 method args { return @_ } 59f2a19305Safresh1 } 60f2a19305Safresh1 61*5486feefSafresh1 my $obj = Testcase4->new; 62f2a19305Safresh1 ok(eq_array([$obj->args("a", "b")], ["a", "b"]), '$self is shifted from @_'); 63f2a19305Safresh1} 64f2a19305Safresh1 65f2a19305Safresh1# anon methods 66f2a19305Safresh1{ 67*5486feefSafresh1 class Testcase5 { 68f2a19305Safresh1 method anonmeth { 69f2a19305Safresh1 return method { 70f2a19305Safresh1 return "Result"; 71f2a19305Safresh1 } 72f2a19305Safresh1 } 73f2a19305Safresh1 } 74f2a19305Safresh1 75*5486feefSafresh1 my $obj = Testcase5->new; 76f2a19305Safresh1 my $mref = $obj->anonmeth; 77f2a19305Safresh1 78f2a19305Safresh1 is($obj->$mref, "Result", 'anon method can be invoked'); 79f2a19305Safresh1} 80f2a19305Safresh1 81f2a19305Safresh1# methods can be forward declared without a body 82f2a19305Safresh1{ 83*5486feefSafresh1 class Testcase6 { 84f2a19305Safresh1 method forwarded; 85f2a19305Safresh1 86f2a19305Safresh1 method forwarded { return "OK" } 87f2a19305Safresh1 } 88f2a19305Safresh1 89*5486feefSafresh1 is(Testcase6->new->forwarded, "OK", 'forward-declared method works'); 90f2a19305Safresh1} 91f2a19305Safresh1 92f2a19305Safresh1done_testing; 93