diff --git a/op.c b/op.c index 8a1907abb1..55c6e1c6de 100644 --- a/op.c +++ b/op.c @@ -10657,6 +10657,8 @@ Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block) } if (block) { + if (CvIsMETHOD(PL_compcv)) + block = class_wrap_method_body(block); /* This makes sub {}; work as expected. */ if (block->op_type == OP_STUB) { const line_t l = PL_parser->copline; diff --git a/t/class/method.t b/t/class/method.t index 96dcf54e2c..58e4f6487c 100644 --- a/t/class/method.t +++ b/t/class/method.t @@ -106,8 +106,10 @@ no warnings 'experimental::class'; # ->& operator can invoke methods with lexical scope { class Testcase8 { + field $f = "private-result"; + my method priv { - return "private-result"; + return $f; } method notpriv { @@ -134,4 +136,22 @@ no warnings 'experimental::class'; '->&m operator does not follow inheritance'); } +# lexical methods with signatures work correctly (GH#23030) +{ + class Testcase9 { + field $x = 123; + + my method priv ( $y ) { + return "X is $x and Y is $y for $self"; + } + + method test { + $self->&priv(456); + } + } + + like(Testcase9->new->test, qr/^X is 123 and Y is 456 for Testcase9=OBJECT\(0x.*\)$/, + 'lexical method with signature counts $self correctly'); +} + done_testing;