| 1 | package Data::Text; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use autodie qw(:all); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use Encode (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use Lingua::Conjunction (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use Object::Configure 0.23; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use Params::Get 0.15; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 10 | ▶ | use String::Util (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | Data::Text - Class to handle text in an OO way | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | Version 0.20 | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 10 | our $VERSION = '0.20'; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 10 | ▶ | use overload ( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | '==' => \&equal, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | '!=' => \¬_equal, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | '""' => \&as_string, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # bool => sub { defined $_[0] && defined $_[0]->{'text'} && length $_[0]->{'text'} }, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 6 | ▶ | bool => sub { 1 }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 10 | fallback => 1 # Prevents boolean context from invoking as_string | |||||||||||||||||||||||||||||||||||||||||
| 35 | 10 | ); | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | C<Data::Text> provides an object-oriented interface for managing and manipulating text content in Perl. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | It wraps string operations in a class-based structure, | ||||||||||||||||||||||||||||||||||||||||||
| 41 | enabling clean chaining of methods like appending, trimming, replacing words, and joining text with conjunctions. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | It supports flexible input types, | ||||||||||||||||||||||||||||||||||||||||||
| 43 | including strings, arrays, and other C<Data::Text> objects, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | and overloads common operators to allow intuitive comparisons and stringification. | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | use Data::Text; | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | my $d = Data::Text->new("Hello, World!\n"); | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | print $d->as_string(); | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | Creates a Data::Text object. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | The optional parameter contains a string or object to initialise the object with. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 172 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 172 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 172 | my $self; | |||||||||||||||||||||||||||||||||||||||||
| 67 | my $params; | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 172 | ▶ | if(@_ == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 135 | $params = Params::Get::get_params('text', \@_); | |||||||||||||||||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 37 | ▶ | $params = Params::Get::get_params(undef, \@_) // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
Params::Get::get_params(undef, \@_) // {}
MC/DC100%1/1
Params::Get::get_params(undef, \@_) // {}
Params::Get::get_params(undef, \@_)
Truth table100%2/2
A
A= Params::Get::get_params(undef, \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 172 | ▶ | if(!defined($class)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | $self = bless {}, __PACKAGE__; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } elsif(blessed($class)) { | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 4 | $self = bless {}, ref($class); | |||||||||||||||||||||||||||||||||||||||||
| 79 | 4 | ▶ | return $self->set($class) if !keys %{$params}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 167 | $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 167 | $params = Object::Configure::configure($class, $params); | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 169 | ▶ | $self->set($params) if $params->{'text'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 169 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | =head2 set | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | Sets the object to contain the given text. | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | The argument can be a reference to an array of strings, or an object. | ||||||||||||||||||||||||||||||||||||||||||
| 95 | If called with an object, the message as_string() is sent to it for its contents. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | $d->set({ text => "Hello, World!\n" }); | ||||||||||||||||||||||||||||||||||||||||||
| 98 | $d->set(text => [ 'Hello, ', 'World!', "\n" ]); | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 164 | ▶ | sub set { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 164 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 164 | my $params = Params::Get::get_params('text', @_); | |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 163 | ▶ | if(!defined($params->{'text'})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 2 | Carp::carp(__PACKAGE__, ': no text given to set()'); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 161 | @{$self}{'file', 'line'} = (caller(0))[1, 2]; | |||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 161 | ▶ | if(ref($params->{'text'})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 10 | ▶ | if(ref($params->{'text'}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 4 | ▶ | if(scalar(@{$params->{'text'}}) == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | Carp::carp(__PACKAGE__, ': no text given'); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | delete $self->{'text'}; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 3 | for my $text (@{$params->{'text'}}) { | |||||||||||||||||||||||||||||||||||||||||
| 121 | 7 | $self = $self->append($text); | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 3 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } elsif(ref($params->{'text'}) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | Carp::croak(__PACKAGE__, ': set(): text cannot be a hashref'); | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 6 | $self->{'text'} = $params->{'text'}->as_string(); | |||||||||||||||||||||||||||||||||||||||||
| 128 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 151 | $self->{'text'} = $params->{'text'}; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 156 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | =head2 append | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | Adds data given in "text" to the end of the object. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | Contains a simple sanity test for consecutive punctuation. | ||||||||||||||||||||||||||||||||||||||||||
| 139 | I expect I'll improve that. | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | Successive calls to append() can be daisy chained. | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | $d->set('Hello ')->append("World!\n"); | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | The argument can be a reference to an array of strings, or an object. | ||||||||||||||||||||||||||||||||||||||||||
| 146 | If called with an object, the message as_string() is sent to it for its contents. | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 1142 | ▶ | sub append { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
append: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 1142 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 1142 | my $params = Params::Get::get_params('text', @_); | |||||||||||||||||||||||||||||||||||||||||
| 153 | 1140 | my $text = $params->{'text'}; | |||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 1140 | ▶ | unless(defined $text) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 2 | Carp::carp(__PACKAGE__, ': no text given to append()'); | |||||||||||||||||||||||||||||||||||||||||
| 157 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 1138 | my $file = $self->{'file'}; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 1138 | my $line = $self->{'line'}; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 1138 | @{$self}{'file', 'line'} = (caller(0))[1, 2]; | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 1138 | ▶ | if(ref($text)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 3 | ▶ | if(ref($text) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 2 | ▶ | unless(@{$text}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 1 | Carp::carp(__PACKAGE__, ': no text given'); | |||||||||||||||||||||||||||||||||||||||||
| 168 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 1 | $self->append($_) for @{$text}; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 1 | $text = $text->as_string(); | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | # FIXME: handle ending with an abbreviation | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 1136 | ▶ | if($self->{'text'} && ($self->{'text'} =~ /\s*[\.,;]\s*$/) && ($text =~ /^\s*[\.,;]/)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$self->{"text"} and $self->{"text"} =~ /\s*[\.,;]\s*$/ and $text =~ /^\s*[\.,;]/
$self->{"text"} and $self->{"text"} =~ /\s*[\.,;]\s*$/
MC/DC100%3/3
$self->{"text"} and $self->{"text"} =~ /\s*[\.,;]\s*$/ and $text =~ /^\s*[\.,;]/
$self->{"text"}
$self->{"text"} =~ /\s*[\.,;]\s*$/
$text =~ /^\s*[\.,;]/
Truth table100%4/4
A and B and C
A= $self->{"text"}B= $self->{"text"} =~ /\s*[\.,;]\s*$/C= $text =~ /^\s*[\.,;]/
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 7 | ▶ | if(my $logger = $self->{'logger'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | $logger->warn(': attempt to add consecutive punctuation' . | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | "\n\tCurrent = '" . $self->{'text'} . | |||||||||||||||||||||||||||||||||||||||||
| 181 | "' at $line of $file\n\tAppend = '$text'"); | ||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | Carp::carp(__PACKAGE__, | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 7 | ": attempt to add consecutive punctuation\n\tCurrent = '", $self->{'text'}, | |||||||||||||||||||||||||||||||||||||||||
| 185 | "' at $line of $file\n\tAppend = '", $text, "'"); | ||||||||||||||||||||||||||||||||||||||||||
| 186 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 1129 | $self->{'text'} .= $text; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 1136 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | =head2 uppercase | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | Converts the text to uppercase. | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | $d->uppercase(); | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | 6 | ▶ | sub uppercase { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
uppercase: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 6 | ▶ | if(defined($self->{'text'})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 5 | Encode::_utf8_on($self->{'text'}); | |||||||||||||||||||||||||||||||||||||||||
| 206 | 5 | $self->{'text'} = uc($self->{'text'}); | |||||||||||||||||||||||||||||||||||||||||
| 207 | 5 | Encode::_utf8_off($self->{'text'}); | |||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | 6 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | =head2 lowercase | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | Converts the text to lowercase. | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | $d->lowercase(); | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | 3 | ▶ | sub lowercase { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lowercase: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | 3 | ▶ | if(defined($self->{'text'})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 2 | Encode::_utf8_on($self->{'text'}); | |||||||||||||||||||||||||||||||||||||||||
| 226 | 2 | $self->{'text'} = lc($self->{'text'}); | |||||||||||||||||||||||||||||||||||||||||
| 227 | 2 | Encode::_utf8_off($self->{'text'}); | |||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 3 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | =head2 clear | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | Clears the text and resets the internal state. | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | $d->clear(); | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | ▶ | sub clear { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 1 | delete @$self{qw(text file line)}; | |||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | =head2 equal($self, $other) | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | Are two texts the same? | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | my $t1 = Data::Text->new('word'); | ||||||||||||||||||||||||||||||||||||||||||
| 254 | my $t2 = Data::Text->new('word'); | ||||||||||||||||||||||||||||||||||||||||||
| 255 | print ($t1 == $t2), "\n"; # Prints 1 | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | 7 | ▶ | sub equal { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
equal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 7 | my ($self, $other) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 7 | return $self->as_string() eq $other->as_string(); | |||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | =head2 not_equal($self, $other) | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | Are two texts different? | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | my $t1 = Data::Text->new('xyzzy'); | ||||||||||||||||||||||||||||||||||||||||||
| 270 | my $t2 = Data::Text->new('plugh'); | ||||||||||||||||||||||||||||||||||||||||||
| 271 | print ($t1 != $t2), "\n"; # Prints 1 | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 6 | ▶ | sub not_equal { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
not_equal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 6 | my ($self, $other) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | 6 | return $self->as_string() ne $other->as_string(); | |||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | =head2 as_string | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | Returns the text as a string. | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | 103 | ▶ | sub as_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 103 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | 103 | return $self->{'text'}; | |||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | =head2 length | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | Returns the length of the text as an integer. | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | This is actually the number of characters, not the number of bytes. | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 12 | ▶ | sub length { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
length: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 12 | ▶ | return 0 unless defined $self->{'text'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | 9 | my $copy = $self->{'text'}; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 9 | Encode::_utf8_on($copy); | |||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | 9 | return length($copy); | |||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | =head2 trim | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | Removes leading and trailing spaces from the text. | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | 4 | ▶ | sub trim { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trim: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | 4 | $self->{'text'} = String::Util::trim($self->{'text'}); | |||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | =head2 rtrim | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | Removes trailing spaces from the text. | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | 2 | ▶ | sub rtrim { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rtrim: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | 2 | $self->{'text'} = String::Util::rtrim($self->{'text'}); | |||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | =head2 replace($self, $replacements) | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | Replaces multiple words in the text. | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | $d->append('Hello World'); | ||||||||||||||||||||||||||||||||||||||||||
| 345 | $d->replace({ 'Hello' => 'Goodbye', 'World' => 'Universe' }); | ||||||||||||||||||||||||||||||||||||||||||
| 346 | print $d->as_string(), "\n"; # Outputs "Goodbye Universe" | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | 12 | ▶ | sub replace { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replace: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | 12 | my ($self, $replacements) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | 12 | ▶ | if($self->{'text'} && (ref($replacements) eq 'HASH')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"text"} and ref $replacements eq "HASH"
MC/DC50%1/2
$self->{"text"} and ref $replacements eq "HASH"
$self->{"text"}
ref $replacements eq "HASH"
Truth table66%2/3
A and B
A= $self->{"text"}B= ref $replacements eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | 10 | for my $search (keys %{$replacements}) { | |||||||||||||||||||||||||||||||||||||||||
| 355 | 11 | my $replace = $replacements->{$search}; | |||||||||||||||||||||||||||||||||||||||||
| 356 | 11 | $self->{'text'} =~ s/\b\Q$search\E\b/$replace/g; | |||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | 12 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | =head2 appendconjunction | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | Add a list as a conjunction. See L<Lingua::Conjunction> | ||||||||||||||||||||||||||||||||||||||||||
| 366 | Because of the way Data::Text works with quoting, | ||||||||||||||||||||||||||||||||||||||||||
| 367 | this code works | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | my $d1 = Data::Text->new(); | ||||||||||||||||||||||||||||||||||||||||||
| 370 | my $d2 = Data::Text->new('a'); | ||||||||||||||||||||||||||||||||||||||||||
| 371 | my $d3 = Data::Text->new('b'); | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | # Prints "a and b\n" | ||||||||||||||||||||||||||||||||||||||||||
| 374 | print $d1->appendconjunction($d2, $d3)->append("\n"); | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | 6 | ▶ | sub appendconjunction { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
appendconjunction: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | 6 | return $self->append(Lingua::Conjunction::conjunction(@_)); | |||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | There is limited Unicode or UTF-8 support. | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||
| 396 | =item * <Test Coverage Report|https://nigelhorne.github.io/Data-Text/coverage/> | ||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | =item * L<String::Util>, L<Lingua::String> | ||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | This module is provided as-is without any warranty. | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | perldoc Data::Text | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | =item * MetaCPAN | ||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | L<https://metacpan.org/release/Data-Text> | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Text> | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | =item * CPAN Testers' Matrix | ||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | L<http://matrix.cpantesters.org/?dist=Data-Text> | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | =item * CPAN Testers Dependencies | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | L<http://deps.cpantesters.org/?module=Data::Text> | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | Copyright 2021-2026 Nigel Horne. | ||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | Usage is subject to the GPL2 licence terms. | ||||||||||||||||||||||||||||||||||||||||||
| 437 | If you use it, | ||||||||||||||||||||||||||||||||||||||||||
| 438 | please let me know. | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||