blib/lib/Object/Pad/LexicalMethods.pm

filter: Statementstmt 83.3% 20 / 24 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 6 / 8 Podpod 0.0% 0 / 3 totaltotal 71.7% 28 / 39 CC 3 SCAR 6.0
CC
3
Cov
79%
apply
3.1
unimport
2.0
unimport_into
2.0
CRAP 1.8
↑ Summary lib/Object/Pad/LexicalMethods.xs »
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 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 require XSLoader;
14 3 XSLoader::load( __PACKAGE__, our $VERSION );
15  
16 =head1 NAME
17  
18 C<Object::Pad::LexicalMethods> - 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<my>). L<Object::Pad> 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<XS::Parse::Infix>
57 describes the situation in more detail.
58  
59 =cut
60  
61 sub import
62 2 ▶{
Subroutine import: called CC=1 SCAR=0.0
63 2 my $pkg = shift;
64 2 my $caller = caller;
65  
66 2 $pkg->import_into( $caller, @_ );
67 }
68  
69 sub unimport
70 0 ▶{
Subroutine unimport: not called CC=1 SCAR=6.9
71 0 my $pkg = shift;
72 0 my $caller = caller;
73  
74 0 $pkg->unimport_into( $caller, @_ );
75 }
76  
77 2 ▶sub import_into { shift->apply( 1, @_ ) }
Subroutine import_into: called CC=1 SCAR=0.0
Pod undocumented
78 0 ▶sub unimport_into { shift->apply( 0, @_ ) }
Subroutine unimport_into: not called CC=1 SCAR=6.9
Pod undocumented
79  
80 sub apply
81 2 ▶{
Subroutine apply: called CC=3 SCAR=11.3
Pod undocumented
82 2 my $pkg = shift;
83 2 my ( $on, $caller, @syms ) = @_;
84  
85 2 ▶ @syms or @syms = qw( ->& );
Branch: 1 branch
BranchTrueFalseTotal
@syms or @syms = "->&" 0 2 2
86  
87 2 $pkg->XS::Parse::Infix::apply_infix( $on, \@syms, qw( ->& ) );
88  
89 2 ▶ croak "Unrecognised import symbols @syms" if @syms;
Branch: 1 branch
BranchTrueFalseTotal
if @syms 0 2 2
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<lexical> 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 <leonerd@leonerd.org.uk>
119  
120 =cut
121  
122 3 0x55AA;
↑ Summary lib/Object/Pad/LexicalMethods.xs »