File Coverage

blib/lib/Object/Pad/Operator/Of.pm
Criterion Covered Total %
statement 20 24 83.3
branch 2 4 50.0
condition n/a
subroutine 7 9 77.7
pod 0 3 0.0
total 29 40 72.5


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::Operator::Of 0.01;
7              
8 2     2   606314 use v5.14;
  2         7  
9 2     2   31 use warnings;
  2         3  
  2         124  
10              
11 2     2   9 use Carp;
  2         3  
  2         140  
12              
13 2     2   1556 use Object::Pad;
  2         23120  
  2         8  
14              
15             require XSLoader;
16             XSLoader::load( __PACKAGE__, our $VERSION );
17              
18             =head1 NAME
19              
20             C - access fields of other instances
21              
22             =head1 SYNOPSIS
23              
24             On Perl v5.38 or later:
25              
26             use v5.38;
27             use Object::Pad;
28             use Object::Pad::Operator::Of;
29              
30             class Bucket {
31             field $size :param;
32              
33             use overload '<=>' => method ($other, $) {
34             return $size <=> $size of $other;
35             };
36             }
37              
38             =head1 DESCRIPTION
39              
40             This module provides an infix operator for accessing fields of other instances
41             of an L class, even if those fields do not have accessor methods.
42             This allows code to be written that can look into the inner workings of other
43             instances of the same class (or subclasses thereof), in order to implement
44             particular behaviours, such as sorting comparisons.
45              
46             Support for custom infix operators was added in the Perl 5.37.x development
47             cycle and is available from development release v5.37.7 onwards, and therefore
48             in Perl v5.38 onwards. The documentation of L
49             describes the situation in more detail.
50              
51             =cut
52              
53             sub import
54             {
55 1     1   14 my $pkg = shift;
56 1         4 my $caller = caller;
57              
58 1         5 $pkg->import_into( $caller, @_ );
59             }
60              
61             sub unimport
62             {
63 0     0   0 my $pkg = shift;
64 0         0 my $caller = caller;
65              
66 0         0 $pkg->unimport_into( $caller, @_ );
67             }
68              
69 1     1 0 6 sub import_into { shift->apply( 1, @_ ) }
70 0     0 0 0 sub unimport_into { shift->apply( 0, @_ ) }
71              
72             sub apply
73             {
74 1     1 0 1 my $pkg = shift;
75 1         6 my ( $on, $caller, @syms ) = @_;
76              
77 1 50       5 @syms or @syms = qw( of );
78              
79 1         8 $pkg->XS::Parse::Infix::apply_infix( $on, \@syms, qw( of ) );
80              
81 1 50       142 croak "Unrecognised import symbols @syms" if @syms;
82             }
83              
84             =head1 OPERATORS
85              
86             =head2 of
87              
88             my $value = $field of $other;
89              
90             Yields the current value of the given field of a different instance than
91             C<$self>. The field variable, on the left of the operator, must be lexically
92             visible in the current scope. The expression on the right must yield an
93             instance of the class that defines the field (or some subclass of it); if not
94             an exception is thrown.
95              
96             =cut
97              
98             =head1 TODO
99              
100             =over 4
101              
102             =item *
103              
104             Try to find a better operator name, which puts the object instance on the left
105             and the field on the right.
106              
107             =item *
108              
109             Look into whether regular Cs can also use C expressions. Currently
110             no fields are visible due to the way that C implements methods.
111              
112             =item *
113              
114             Look into whether it might be possible to use C expressions as lvalues,
115             for mutation or assignment.
116              
117             =back
118              
119             =cut
120              
121             =head1 AUTHOR
122              
123             Paul Evans
124              
125             =cut
126              
127             0x55AA;