File Coverage

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


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-2012 -- leonerd@leonerd.org.uk
5              
6             package overload::substr;
7              
8 6     6   130139 use strict;
  6         12  
  6         191  
9 6     6   33 use warnings;
  6         8  
  6         200  
10              
11 6     6   34 use Carp;
  6         13  
  6         1328  
12              
13             our $VERSION = '0.03';
14              
15             require XSLoader;
16             XSLoader::load(__PACKAGE__, $VERSION );
17              
18             =head1 NAME
19              
20             C - overload Perl's C function
21              
22             =head1 SYNOPSIS
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 5     5   42 my $class = shift;
78 5         16 my %args = @_;
79              
80 5         12 my $package = caller;
81              
82 5         12 my $substr = delete $args{substr};
83 5 100       28 defined $substr or $substr = "_substr";
84              
85 5 50       26 keys %args and
86             croak "Unrecognised extra keys to $class: " . join( ", ", sort keys %args );
87              
88 6     6   31 no strict 'refs';
  6         9  
  6         556  
89              
90 5 100       25 unless( ref $substr ) {
91 3         4 $substr = \&{$package."::$substr"};
  3         24  
92             }
93              
94             # This somewhat steps on overload.pm 's toes
95 5         10 *{$package."::(substr"} = $substr;
  5         6168  
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;