Skip to content

Commit

Permalink
Add docs and perldelta for ->& operator
Browse files Browse the repository at this point in the history
  • Loading branch information
leonerd committed Feb 10, 2025
1 parent fd64148 commit d4ad677
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
10 changes: 6 additions & 4 deletions pod/perlclass.pod
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,22 @@ but does not appear in the symbol table. The effect is that of a I<private>
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) {
say "Internal method abc invoked with x=$x y=$y";
}

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
Expand Down
11 changes: 10 additions & 1 deletion pod/perldelta.pod
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ here, but most should go in the L</Performance Enhancements> section.

[ List each enhancement as a =head2 entry ]

=head2 Lexical method syntax using C<my method>
=head2 Lexical method declaration using C<my method>

Like C<sub> since Perl version 5.18, C<method> can now be prefixed with the
C<my> keyword. This declares a subroutine that has lexical, rather than
package visibility. See L<perlclass> 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
Expand Down
6 changes: 6 additions & 0 deletions pod/perlop.pod
Original file line number Diff line number Diff line change
Expand Up @@ -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<perlobj>.

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<postderef> feature. For the
details of that feature, consult L<perlref/Postfix Dereference Syntax>.
Expand Down

0 comments on commit d4ad677

Please sign in to comment.