diff --git a/pod/perlclass.pod b/pod/perlclass.pod index aa69f9b2bb27..298ff6e85846 100644 --- a/pod/perlclass.pod +++ b/pod/perlclass.pod @@ -191,9 +191,9 @@ but does not appear in the symbol table. The effect is that of a I method; one that can be called from within the class's own code, but not from outside. -At present, there is no special syntax for invoking a lexical method, so for -now they must be called as if they are regular (lexical) subroutines, -remembering to pass the C<$self> invocant as the first positional argument. +To invoke these lexical subroutines as methods, it is best to use the +C<< ->& >> operator. This bypasses method lookup by name, and directly +invokes a lexical subroutine as if it was a method. class LexicalMethod { my method abc ($x, $y) { @@ -201,10 +201,12 @@ remembering to pass the C<$self> invocant as the first positional argument. } method xyz { - abc($self, "x", "y"); + $self->&abc("x", "y"); } } + # The `abc` method is not visible from here + =head1 ATTRIBUTES Specific aspects of the keywords mentioned above are managed using diff --git a/pod/perldelta.pod b/pod/perldelta.pod index 9aed6616e361..a055b289e56f 100644 --- a/pod/perldelta.pod +++ b/pod/perldelta.pod @@ -27,12 +27,21 @@ here, but most should go in the L section. [ List each enhancement as a =head2 entry ] -=head2 Lexical method syntax using C +=head2 Lexical method declaration using C Like C since Perl version 5.18, C can now be prefixed with the C keyword. This declares a subroutine that has lexical, rather than package visibility. See L for more detail. +=head2 Lexical method invocation operator C<< ->& >> + +Along with the ability to declare methods lexically, this release also permits +invoking a lexical subroutine as if it were a method, bypassing the usual +name-based method resolution by name. + +Combined with lexical method declaration, these two new abilities create the +effect of having private methods. + =head1 Security XXX Any security-related notices go here. In particular, any security diff --git a/pod/perlop.pod b/pod/perlop.pod index 7af391216a80..925f2824b60a 100644 --- a/pod/perlop.pod +++ b/pod/perlop.pod @@ -236,6 +236,12 @@ and (if it is a method name) the left side must be either an object (a blessed reference) or a class name (that is, a package name). See L. +The right side may also be the name of a subroutine, prefixed with the C<&> +sigil. This creates what looks like a lexical method invocation, where the +method subroutine is resolved lexically instead of by name by a search within +the packages of the object's class. This resolution happens entirely at +compile-time, and performs the same as a regular subroutine call at runtime. + The dereferencing cases (as opposed to method-calling cases) are somewhat extended by the C feature. For the details of that feature, consult L.