| 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 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 6 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 6 | our $VERSION = '0.03'; | |||||||||
| 14 | |||||||||||
| 15 | 6 | require XSLoader; | |||||||||
| 16 | 6 | XSLoader::load(__PACKAGE__, $VERSION ); | |||||||||
| 17 | |||||||||||
| 18 | =head1 NAME | ||||||||||
| 19 | |||||||||||
| 20 | C<overload::substr> - overload Perl's C<substr()> 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<substr> core function, | ||||||||||
| 44 | which Perl's C<overload> pragma does not allow by itself. | ||||||||||
| 45 | |||||||||||
| 46 | It is invoked similarly to the C<overload> pragma, being passed a single named | ||||||||||
| 47 | argument which should be a code reference or method name to implement the | ||||||||||
| 48 | C<substr> 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<substr>; 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<substr> | ||||||||||
| 63 | function that invoked it. | ||||||||||
| 64 | |||||||||||
| 65 | If the C<substr> 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<substr> 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 | ▶ | my $class = shift; | ||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||
| 78 | 5 | my %args = @_; | |||||||||
| 79 | |||||||||||
| 80 | 5 | my $package = caller; | |||||||||
| 81 | |||||||||||
| 82 | 5 | my $substr = delete $args{substr}; | |||||||||
| 83 | 5 | ▶ | defined $substr or $substr = "_substr"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | |||||||||||
| 85 | 5 | ▶ | keys %args and | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | croak "Unrecognised extra keys to $class: " . join( ", ", sort keys %args ); | ||||||||||
| 87 | |||||||||||
| 88 | 6 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 89 | |||||||||||
| 90 | 5 | ▶ | unless( ref $substr ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 3 | $substr = \&{$package."::$substr"}; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | # This somewhat steps on overload.pm 's toes | ||||||||||
| 95 | 5 | *{$package."::(substr"} = $substr; | |||||||||
| 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<m//> and C<s///> 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 <mst@shadowcat.co.uk> for suggesting the | ||||||||||
| 124 | possibility, and Joshua ben Jore <jjore@cpan.org> for the inspiration by way | ||||||||||
| 125 | of L<UNIVERSAL::ref>. | ||||||||||
| 126 | |||||||||||
| 127 | =head1 AUTHOR | ||||||||||
| 128 | |||||||||||
| 129 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 130 | |||||||||||
| 131 | =cut | ||||||||||
| 132 | |||||||||||
| 133 | 6 | 0x55AA; | |||||||||