| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# You may distribute under the terms of either the GNU General Public License |
|
2
|
|
|
|
|
|
|
# or the Artistic License (the same terms as Perl itself) |
|
3
|
|
|
|
|
|
|
# |
|
4
|
|
|
|
|
|
|
# (C) Paul Evans, 2024 -- leonerd@leonerd.org.uk |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Object::Pad::LexicalMethods 0.01; |
|
7
|
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
841541
|
use v5.14; |
|
|
3
|
|
|
|
|
13
|
|
|
9
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
160
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
|
22
|
use Carp; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
1142
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
require XSLoader; |
|
14
|
|
|
|
|
|
|
XSLoader::load( __PACKAGE__, our $VERSION ); |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
C - operator for lexical method call syntax |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=for highlighter language=perl |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use v5.38; |
|
25
|
|
|
|
|
|
|
use Object::Pad; |
|
26
|
|
|
|
|
|
|
use Object::Pad::LexicalMethods; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
class WithPrivate { |
|
29
|
|
|
|
|
|
|
field $var; |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my method inc_var { $var++ } |
|
32
|
|
|
|
|
|
|
my method dec_var { $var-- } |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
method bump { |
|
35
|
|
|
|
|
|
|
$self->&inc_var; |
|
36
|
|
|
|
|
|
|
say "In the middle"; |
|
37
|
|
|
|
|
|
|
$self->&dec_var; |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
} |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Perl version 5.18 added lexical subroutines, which are located in the lexical |
|
44
|
|
|
|
|
|
|
scope (much like variables declared with C). L version 0.814 |
|
45
|
|
|
|
|
|
|
supports methods being declared lexically as well, meaning they do not appear |
|
46
|
|
|
|
|
|
|
in the package namespace of the class, and are not accessible from other |
|
47
|
|
|
|
|
|
|
scopes. However, Perl does not currently provide a method call syntax for |
|
48
|
|
|
|
|
|
|
invoking these from the lexical scope while looking like method calls. |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
This module provides an infix operator for making the syntax of calls to |
|
51
|
|
|
|
|
|
|
lexical subroutines as if they were methods defined on an object instance look |
|
52
|
|
|
|
|
|
|
more like named method dispatch syntax. |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Support for custom infix operators was added in the Perl 5.37.x development |
|
55
|
|
|
|
|
|
|
cycle and is available from development release v5.37.7 onwards, and therefore |
|
56
|
|
|
|
|
|
|
in Perl v5.38 onwards. The documentation of L |
|
57
|
|
|
|
|
|
|
describes the situation in more detail. |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=cut |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub import |
|
62
|
|
|
|
|
|
|
{ |
|
63
|
2
|
|
|
2
|
|
20
|
my $pkg = shift; |
|
64
|
2
|
|
|
|
|
5
|
my $caller = caller; |
|
65
|
|
|
|
|
|
|
|
|
66
|
2
|
|
|
|
|
7
|
$pkg->import_into( $caller, @_ ); |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub unimport |
|
70
|
|
|
|
|
|
|
{ |
|
71
|
0
|
|
|
0
|
|
0
|
my $pkg = shift; |
|
72
|
0
|
|
|
|
|
0
|
my $caller = caller; |
|
73
|
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
0
|
$pkg->unimport_into( $caller, @_ ); |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
2
|
|
|
2
|
0
|
5
|
sub import_into { shift->apply( 1, @_ ) } |
|
78
|
0
|
|
|
0
|
0
|
0
|
sub unimport_into { shift->apply( 0, @_ ) } |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub apply |
|
81
|
|
|
|
|
|
|
{ |
|
82
|
2
|
|
|
2
|
0
|
3
|
my $pkg = shift; |
|
83
|
2
|
|
|
|
|
5
|
my ( $on, $caller, @syms ) = @_; |
|
84
|
|
|
|
|
|
|
|
|
85
|
2
|
50
|
|
|
|
10
|
@syms or @syms = qw( ->& ); |
|
86
|
|
|
|
|
|
|
|
|
87
|
2
|
|
|
|
|
14
|
$pkg->XS::Parse::Infix::apply_infix( $on, \@syms, qw( ->& ) ); |
|
88
|
|
|
|
|
|
|
|
|
89
|
2
|
50
|
|
|
|
220
|
croak "Unrecognised import symbols @syms" if @syms; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 OPERATORS |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 ->& |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
@result = $instance->&lexmethod( @args ); |
|
97
|
|
|
|
|
|
|
@result = $instance->&lexmethod; |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Invokes a lexical subroutine (that must be visible in the current scope) as if |
|
100
|
|
|
|
|
|
|
it were a method on instance given by the LHS operand. Arguments may be |
|
101
|
|
|
|
|
|
|
passed; if so they must be surrounded by parentheses. |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
This is exactly equivalent to simply invoking the subroutine as a plain |
|
104
|
|
|
|
|
|
|
function and passing in the instance as the first argument. However, the |
|
105
|
|
|
|
|
|
|
syntax looks more like regular name-based dispatch method invocation, and is |
|
106
|
|
|
|
|
|
|
perhaps less surprising to readers as a result. Also, this operator will only |
|
107
|
|
|
|
|
|
|
accept I subroutines as methods; it will reject package-named ones |
|
108
|
|
|
|
|
|
|
that would otherwise be visible here. |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Note that as this is implemented as a single infix operator named C<< ->& >> |
|
111
|
|
|
|
|
|
|
whitespace is not permitted after the arrow but before the ampersand, whereas |
|
112
|
|
|
|
|
|
|
other arrow-like operators in Perl (such as C<< ->[ ... ] >>) do permit this. |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=cut |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 AUTHOR |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Paul Evans |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=cut |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
0x55AA; |