line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Trim; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
370494
|
use strict; |
|
6
|
|
|
|
|
50
|
|
|
6
|
|
|
|
|
151
|
|
4
|
6
|
|
|
6
|
|
24
|
use warnings; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
363
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Text::Trim - remove leading and/or trailing whitespace from strings |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
version 1.03 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '1.03'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use Text::Trim; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
$text = "\timportant data\n"; |
23
|
|
|
|
|
|
|
$data = trim $text; |
24
|
|
|
|
|
|
|
# now $data contains "important data" and $text is unchanged |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# or: |
27
|
|
|
|
|
|
|
trim $text; # work in-place, $text now contains "important data" |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
@lines = <STDIN>; |
30
|
|
|
|
|
|
|
rtrim @lines; # remove trailing whitespace from all lines |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Alternatively: |
33
|
|
|
|
|
|
|
@lines = rtrim <STDIN>; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Or even: |
36
|
|
|
|
|
|
|
while (<STDIN>) { |
37
|
|
|
|
|
|
|
trim; # Change $_ in place |
38
|
|
|
|
|
|
|
# ... |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 DESCRIPTION |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
This module provides functions for removing leading and/or trailing whitespace |
44
|
|
|
|
|
|
|
from strings. It is basically a wrapper around some simple regexes with a |
45
|
|
|
|
|
|
|
flexible context-based interface. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 EXPORTS |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
All functions are exported by default. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
52
|
|
|
|
|
|
|
|
53
|
6
|
|
|
6
|
|
33
|
use Exporter; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
2484
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
our @ISA = qw( Exporter ); |
56
|
|
|
|
|
|
|
our @EXPORT = qw( rtrim ltrim trim ); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 CONTEXT HANDLING |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head2 void context |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Functions called in void context change their arguments in-place |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
trim(@strings); # All strings in @strings are trimmed in-place |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
ltrim($text); # remove leading whitespace on $text |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
rtrim; # remove trailing whitespace on $_ |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
No changes are made to arguments in non-void contexts. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 list context |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Values passed in are changed and returned without affecting the originals. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
@result = trim(@strings); # @strings is unchanged |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
@result = rtrim; # @result contains rtrimmed $_ |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
($result) = ltrim(@strings); # like $result = ltrim($strings[0]); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 scalar context |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
As list context but multiple arguments are stringified before being returned. |
85
|
|
|
|
|
|
|
Single arguments are unaffected. This means that under these circumstances, |
86
|
|
|
|
|
|
|
the value of C<$"> (C<$LIST_SEPARATOR>) is used to join the values. If you |
87
|
|
|
|
|
|
|
don't want this, make sure you only use single arguments when calling in |
88
|
|
|
|
|
|
|
scalar context. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
@strings = ("\thello\n", "\tthere\n"); |
91
|
|
|
|
|
|
|
$trimmed = trim(@strings); |
92
|
|
|
|
|
|
|
# $trimmed = "hello there" |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
local $" = ', '; |
95
|
|
|
|
|
|
|
$trimmed = trim(@strings); |
96
|
|
|
|
|
|
|
# Now $trimmed = "hello, there" |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
$trimmed = rtrim; |
99
|
|
|
|
|
|
|
# $trimmed = $_ minus trailing whitespace |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head2 Undefined values |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
If any of the functions are called with undefined values, the behaviour is in |
104
|
|
|
|
|
|
|
general to pass them through unchanged. When stringifying a list (calling in |
105
|
|
|
|
|
|
|
scalar context with multiple arguments) undefined elements are excluded, but |
106
|
|
|
|
|
|
|
if all elements are undefined then the return value is also undefined. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$foo = trim(undef); # $foo is undefined |
109
|
|
|
|
|
|
|
$foo = trim(undef, undef); # $foo is undefined |
110
|
|
|
|
|
|
|
@foo = trim(undef, undef); # @foo contains 2 undefined values |
111
|
|
|
|
|
|
|
trim(@foo) # @foo still contains 2 undefined values |
112
|
|
|
|
|
|
|
$foo = trim('', undef); # $foo is '' |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 FUNCTIONS |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 trim |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Removes leading and trailing whitespace from all arguments, or C<$_> if none |
119
|
|
|
|
|
|
|
are provided. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub trim { |
124
|
14
|
100
|
|
14
|
1
|
28736
|
@_ = @_ ? @_ : $_ if defined wantarray; |
|
|
100
|
|
|
|
|
|
125
|
|
|
|
|
|
|
|
126
|
14
|
100
|
|
|
|
45
|
for (@_ ? @_ : $_) { next unless defined; s/\A\s+//; s/\s+\z// } |
|
32
|
100
|
|
|
|
50
|
|
|
24
|
|
|
|
|
71
|
|
|
24
|
|
|
|
|
75
|
|
127
|
|
|
|
|
|
|
|
128
|
14
|
100
|
100
|
|
|
65
|
return @_ if wantarray || !defined wantarray; |
129
|
|
|
|
|
|
|
|
130
|
6
|
100
|
|
|
|
30
|
if (my @def = grep defined, @_) { return "@def" } else { return } |
|
5
|
|
|
|
|
26
|
|
|
1
|
|
|
|
|
4
|
|
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head2 rtrim |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Like C<trim()> but removes only trailing (right) whitespace. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub rtrim { |
140
|
12
|
100
|
|
12
|
1
|
5761
|
@_ = @_ ? @_ : $_ if defined wantarray; |
|
|
100
|
|
|
|
|
|
141
|
|
|
|
|
|
|
|
142
|
12
|
100
|
|
|
|
32
|
for (@_ ? @_ : $_) { next unless defined; s/\s+\z// } |
|
30
|
100
|
|
|
|
49
|
|
|
22
|
|
|
|
|
86
|
|
143
|
|
|
|
|
|
|
|
144
|
12
|
100
|
100
|
|
|
50
|
return @_ if wantarray || !defined wantarray; |
145
|
|
|
|
|
|
|
|
146
|
5
|
100
|
|
|
|
21
|
if (my @def = grep defined, @_) { return "@def" } else { return } |
|
4
|
|
|
|
|
18
|
|
|
1
|
|
|
|
|
13
|
|
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 ltrim |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Like C<trim()> but removes only leading (left) whitespace. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub ltrim { |
156
|
12
|
100
|
|
12
|
1
|
6547
|
@_ = @_ ? @_ : $_ if defined wantarray; |
|
|
100
|
|
|
|
|
|
157
|
|
|
|
|
|
|
|
158
|
12
|
100
|
|
|
|
31
|
for (@_ ? @_ : $_) { next unless defined; s/\A\s+// } |
|
30
|
100
|
|
|
|
47
|
|
|
22
|
|
|
|
|
65
|
|
159
|
|
|
|
|
|
|
|
160
|
12
|
100
|
100
|
|
|
56
|
return @_ if wantarray || !defined wantarray; |
161
|
|
|
|
|
|
|
|
162
|
5
|
100
|
|
|
|
21
|
if (my @def = grep defined, @_) { return "@def" } else { return } |
|
4
|
|
|
|
|
19
|
|
|
1
|
|
|
|
|
8
|
|
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
1; |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
__END__ |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 UNICODE |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Because this module is implemented using Perl regular expressions, it is capable |
172
|
|
|
|
|
|
|
of recognising and removing unicode whitespace characters (such as non-breaking |
173
|
|
|
|
|
|
|
spaces) from scalars with the utf8 flag on. See L<Encode> for details about the |
174
|
|
|
|
|
|
|
utf8 flag. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Note that this only applies in the case of perl versions after 5.8.0 or so. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 SEE ALSO |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Brent B. Powers' L<String::Strip> performs a similar function in XS. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 AUTHORS |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
B<Matt Lawrence> E<lt>mattlaw@cpan.orgE<gt> - Original author and maintainer |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
B<Ryan Thompson> E<lt>rjt@cpan.orgE<gt> - Co-maintainer, miscellaneous fixes |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 BUGS |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Please report any bugs or feature requests to C<bug-text-trim@rt.cpan.org>, |
191
|
|
|
|
|
|
|
or through the web interface at |
192
|
|
|
|
|
|
|
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-Trim>. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Terrence Brannon E<lt>metaperl@gmail.comE<gt> for bringing my attention to |
197
|
|
|
|
|
|
|
L<String::Strip> and suggesting documentation changes. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 LICENSE |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
This program is free software; you can redistribute it |
202
|
|
|
|
|
|
|
and/or modify it under the same terms as Perl itself. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
L<http://dev.perl.org/licenses/artistic.html> |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=cut |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
vim: ts=8 sts=4 sw=4 sr et |