File Coverage

blib/lib/overload/substr.pm
Criterion Covered Total %
statement 22 22 100.0
branch 5 6 83.3
condition n/a
subroutine 5 5 100.0
pod n/a
total 32 33 96.9


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, 2010-2025 -- leonerd@leonerd.org.uk
5              
6             package overload::substr 0.04;
7              
8 6     6   1681281 use v5.14;
  6         23  
9 6     6   37 use warnings;
  6         54  
  6         403  
10              
11 6     6   37 use Carp;
  6         16  
  6         1446  
12              
13             require XSLoader;
14             XSLoader::load(__PACKAGE__, our $VERSION );
15              
16             =head1 NAME
17              
18             C - overload Perl's C function
19              
20             =head1 SYNOPSIS
21              
22             =for highlighter language=perl
23              
24             package My::Stringlike::Object;
25              
26             use overload::substr;
27              
28             sub _substr
29             {
30             my $self = shift;
31             if( @_ > 2 ) {
32             $self->replace_substr( @_ );
33             }
34             else {
35             return $self->get_substr( @_ );
36             }
37             }
38              
39             ...
40              
41             =head1 DESCRIPTION
42              
43             This module allows an object class to overload the C core function,
44             which Perl's C pragma does not allow by itself.
45              
46             It is invoked similarly to the C pragma, being passed a single named
47             argument which should be a code reference or method name to implement the
48             C function.
49              
50             use overload::substr substr => \&SUBSTR;
51              
52             use overload::substr substr => "SUBSTR";
53              
54             The referred method will be invoked as per core's C; namely, it will
55             take the string to be operated on (which will be an object in this case), an
56             offset, optionally a length, and optionally a replacement.
57              
58             $str->SUBSTR( $offset );
59             $str->SUBSTR( $offset, $length );
60             $str->SUBSTR( $offset, $length, $replacement );
61              
62             In each case, whatever it returns will be the return value of the C
63             function that invoked it.
64              
65             If the C argument is not provided, it defaults to a method called
66             C<_substr>.
67              
68             It is not required that the return value be a plain string; any Perl value may
69             be returned unmodified from the C method, or passed in as the value of
70             the replacement. This allows objects to behave in whatever way is deemed most
71             appropriate.
72              
73             =cut
74              
75             sub import
76             {
77 4     4   33 my $class = shift;
78 4         37 my %args = @_;
79              
80 4         10 my $package = caller;
81              
82 4         11 my $substr = delete $args{substr};
83 4 100       27 defined $substr or $substr = "_substr";
84              
85 4 50       18 keys %args and
86             croak "Unrecognised extra keys to $class: " . join( ", ", sort keys %args );
87              
88 6     6   62 no strict 'refs';
  6         22  
  6         859  
89              
90 4 100       17 unless( ref $substr ) {
91 2         5 $substr = \&{$package."::$substr"};
  2         18  
92             }
93              
94             # This somewhat steps on overload.pm 's toes
95 4         9 *{$package."::(substr"} = $substr;
  4         7119  
96             }
97              
98             =head1 TODO
99              
100             =over 8
101              
102             =item *
103              
104             More testing - edge cases, especially in LVALUE logic.
105              
106             =item *
107              
108             Test for memory leaks, especially in LVALUE logic.
109              
110             =item *
111              
112             Look into / implement fixup of substr() ops compiled before module is loaded
113              
114             =item *
115              
116             Consider if implementations of split(), and C and C regexps should
117             be done that also uses the overloaded substr() method.
118              
119             =back
120              
121             =head1 ACKNOWLEDGEMENTS
122              
123             With thanks to Matt S Trout for suggesting the
124             possibility, and Joshua ben Jore for the inspiration by way
125             of L.
126              
127             =head1 AUTHOR
128              
129             Paul Evans
130              
131             =cut
132              
133             0x55AA;