| 1 | package Data::Perl::Role::String; | ||||||||||||||
| 2 | 9 | $Data::Perl::Role::String::VERSION = '0.002011'; | |||||||||||||
| 3 | # ABSTRACT: Wrapping class for Perl scalar strings. | ||||||||||||||
| 4 | |||||||||||||||
| 5 | 9 | ▶ | use strictures 1; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 9 | ▶ | use Role::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 9 | ▶ | sub new { bless \(my $s = $_[1]), $_[0] } | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | ▶ | sub inc { ++${$_[0]} } | ||||||||||||
|
Subroutine
inc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 1 | ▶ | sub append { ${$_[0]} = ${$_[0]} . $_[1] } | ||||||||||||
|
Subroutine
append: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 1 | ▶ | sub prepend { ${$_[0]} = $_[1] . ${$_[0]} } | ||||||||||||
|
Subroutine
prepend: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 2 | ▶ | sub replace { | ||||||||||||
|
Subroutine
replace: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 18 | 2 | ▶ | if (ref($_[2]) eq 'CODE') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 19 | 1 | ${$_[0]} =~ s/$_[1]/$_[2]->()/e; | |||||||||||||
| 20 | } | ||||||||||||||
| 21 | else { | ||||||||||||||
| 22 | 1 | ${$_[0]} =~ s/$_[1]/$_[2]/; | |||||||||||||
| 23 | } | ||||||||||||||
| 24 | 2 | ${$_[0]}; | |||||||||||||
| 25 | } | ||||||||||||||
| 26 | |||||||||||||||
| 27 | 1 | ▶ | sub match { | ||||||||||||
|
Subroutine
match: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 28 | 1 | ${$_[0]} =~ /$_[1]/; | |||||||||||||
| 29 | } | ||||||||||||||
| 30 | |||||||||||||||
| 31 | 1 | ▶ | sub chop { CORE::chop ${$_[0]} } | ||||||||||||
|
Subroutine
chop: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 32 | |||||||||||||||
| 33 | 1 | ▶ | sub chomp { CORE::chomp ${$_[0]} } | ||||||||||||
|
Subroutine
chomp: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 34 | |||||||||||||||
| 35 | 1 | ▶ | sub clear { ${$_[0]} = '' } | ||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 36 | |||||||||||||||
| 37 | 1 | ▶ | sub length { CORE::length ${$_[0]} } | ||||||||||||
|
Subroutine
length: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 38 | |||||||||||||||
| 39 | 7 | ▶ | sub substr { | ||||||||||||
|
Subroutine
substr: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 40 | 7 | ▶ | if (@_ >= 4) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 41 | 1 | substr ${$_[0]}, $_[1], $_[2], $_[3]; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | elsif (@_ == 3) { | ||||||||||||||
| 44 | 4 | substr ${$_[0]}, $_[1], $_[2]; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | else { | ||||||||||||||
| 47 | 2 | substr ${$_[0]}, $_[1]; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 9 | 1; | |||||||||||||
| 52 | |||||||||||||||
| 53 | =pod | ||||||||||||||
| 54 | |||||||||||||||
| 55 | =encoding UTF-8 | ||||||||||||||
| 56 | |||||||||||||||
| 57 | =head1 NAME | ||||||||||||||
| 58 | |||||||||||||||
| 59 | Data::Perl::Role::String - Wrapping class for Perl scalar strings. | ||||||||||||||
| 60 | |||||||||||||||
| 61 | =head1 VERSION | ||||||||||||||
| 62 | |||||||||||||||
| 63 | version 0.002011 | ||||||||||||||
| 64 | |||||||||||||||
| 65 | =head1 SYNOPSIS | ||||||||||||||
| 66 | |||||||||||||||
| 67 | use Data::Perl qw/string/; | ||||||||||||||
| 68 | |||||||||||||||
| 69 | my $string = string("foo\n"); | ||||||||||||||
| 70 | |||||||||||||||
| 71 | $string->chomp; # returns 1, $string == "foo" | ||||||||||||||
| 72 | |||||||||||||||
| 73 | =head1 DESCRIPTION | ||||||||||||||
| 74 | |||||||||||||||
| 75 | This class provides a wrapper and methods for interacting with scalar strings. | ||||||||||||||
| 76 | |||||||||||||||
| 77 | =head1 PROVIDED METHODS | ||||||||||||||
| 78 | |||||||||||||||
| 79 | =over 4 | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =item * B<new($value)> | ||||||||||||||
| 82 | |||||||||||||||
| 83 | Constructs a new Data::Perl::String object, optionally initialized to $value if | ||||||||||||||
| 84 | passed in, and returns it. | ||||||||||||||
| 85 | |||||||||||||||
| 86 | =item * B<inc> | ||||||||||||||
| 87 | |||||||||||||||
| 88 | Increments the value stored in this slot using the magical string autoincrement | ||||||||||||||
| 89 | operator. Note that Perl doesn't provide analogous behavior in C<-->, so | ||||||||||||||
| 90 | C<dec> is not available. This method returns the new value. | ||||||||||||||
| 91 | |||||||||||||||
| 92 | This method does not accept any arguments. | ||||||||||||||
| 93 | |||||||||||||||
| 94 | =item * B<append($string)> | ||||||||||||||
| 95 | |||||||||||||||
| 96 | Appends to the string, like C<.=>, and returns the new value. | ||||||||||||||
| 97 | |||||||||||||||
| 98 | This method requires a single argument. | ||||||||||||||
| 99 | |||||||||||||||
| 100 | =item * B<prepend($string)> | ||||||||||||||
| 101 | |||||||||||||||
| 102 | Prepends to the string and returns the new value. | ||||||||||||||
| 103 | |||||||||||||||
| 104 | This method requires a single argument. | ||||||||||||||
| 105 | |||||||||||||||
| 106 | =item * B<replace($pattern, $replacement)> | ||||||||||||||
| 107 | |||||||||||||||
| 108 | Performs a regexp substitution (L<perlop/s>). There is no way to provide the | ||||||||||||||
| 109 | C<g> flag, but code references will be accepted for the replacement, causing | ||||||||||||||
| 110 | the regex to be modified with a single C<e>. C</smxi> can be applied using the | ||||||||||||||
| 111 | C<qr> operator. This method returns the new value. | ||||||||||||||
| 112 | |||||||||||||||
| 113 | This method requires two arguments. | ||||||||||||||
| 114 | |||||||||||||||
| 115 | =item * B<match($pattern)> | ||||||||||||||
| 116 | |||||||||||||||
| 117 | Runs the regex against the string and returns the matching value(s). | ||||||||||||||
| 118 | |||||||||||||||
| 119 | This method requires a single argument. | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =item * B<chop> | ||||||||||||||
| 122 | |||||||||||||||
| 123 | Just like L<perlfunc/chop>. This method returns the chopped character. | ||||||||||||||
| 124 | |||||||||||||||
| 125 | This method does not accept any arguments. | ||||||||||||||
| 126 | |||||||||||||||
| 127 | =item * B<chomp> | ||||||||||||||
| 128 | |||||||||||||||
| 129 | Just like L<perlfunc/chomp>. This method returns the number of characters | ||||||||||||||
| 130 | removed. | ||||||||||||||
| 131 | |||||||||||||||
| 132 | This method does not accept any arguments. | ||||||||||||||
| 133 | |||||||||||||||
| 134 | =item * B<clear> | ||||||||||||||
| 135 | |||||||||||||||
| 136 | Sets the string to the empty string (not the value passed to C<default>). | ||||||||||||||
| 137 | |||||||||||||||
| 138 | This method does not have a defined return value. | ||||||||||||||
| 139 | |||||||||||||||
| 140 | This method does not accept any arguments. | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =item * B<length> | ||||||||||||||
| 143 | |||||||||||||||
| 144 | Just like L<perlfunc/length>, returns the length of the string. | ||||||||||||||
| 145 | |||||||||||||||
| 146 | =item * B<substr> | ||||||||||||||
| 147 | |||||||||||||||
| 148 | This acts just like L<perlfunc/substr>. When called as a writer, it returns | ||||||||||||||
| 149 | the substring that was replaced, just like the Perl builtin. | ||||||||||||||
| 150 | |||||||||||||||
| 151 | This method requires at least one argument, and accepts no more than three. | ||||||||||||||
| 152 | |||||||||||||||
| 153 | =back | ||||||||||||||
| 154 | |||||||||||||||
| 155 | =head1 SEE ALSO | ||||||||||||||
| 156 | |||||||||||||||
| 157 | =over 4 | ||||||||||||||
| 158 | |||||||||||||||
| 159 | =item * L<Data::Perl> | ||||||||||||||
| 160 | |||||||||||||||
| 161 | =item * L<MooX::HandlesVia> | ||||||||||||||
| 162 | |||||||||||||||
| 163 | =back | ||||||||||||||
| 164 | |||||||||||||||
| 165 | =head1 AUTHOR | ||||||||||||||
| 166 | |||||||||||||||
| 167 | Matthew Phillips <mattp@cpan.org> | ||||||||||||||
| 168 | |||||||||||||||
| 169 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 170 | |||||||||||||||
| 171 | This software is copyright (c) 2020 by Matthew Phillips <mattp@cpan.org>. | ||||||||||||||
| 172 | |||||||||||||||
| 173 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 174 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 175 | |||||||||||||||
| 176 | =cut | ||||||||||||||
| 177 | |||||||||||||||
| 178 | __END__ | ||||||||||||||