line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package String::Trim; |
2
|
|
|
|
|
|
|
# ABSTRACT: trim whitespace from your strings |
3
|
8
|
|
|
8
|
|
390549
|
use strict; |
|
8
|
|
|
|
|
22
|
|
|
8
|
|
|
|
|
420
|
|
4
|
8
|
|
|
8
|
|
39
|
use warnings; |
|
8
|
|
|
|
|
14
|
|
|
8
|
|
|
|
|
415
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.005'; # VERSION |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
8
|
8
|
|
|
8
|
|
38
|
use Exporter 5.57 qw(import); |
|
8
|
|
|
|
|
183
|
|
|
8
|
|
|
|
|
5593
|
|
9
|
|
|
|
|
|
|
our @EXPORT = qw(trim); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub trim { # Starting point: http://www.perlmonks.org/?node_id=36684 |
13
|
36
|
|
|
36
|
1
|
40548
|
my $t = qr{^\s+|\s+$}; |
14
|
36
|
100
|
|
|
|
114
|
if (defined wantarray) { |
15
|
15
|
50
|
|
|
|
58
|
@_ = (@_ ? @_ : $_); |
16
|
15
|
100
|
|
|
|
80
|
if (ref $_[0] eq 'ARRAY') { |
|
|
100
|
|
|
|
|
|
17
|
2
|
|
|
|
|
2
|
@_ = @{ $_[0] }; |
|
2
|
|
|
|
|
6
|
|
18
|
2
|
100
|
|
|
|
7
|
for (@_) { s{$t}{}g if defined $_ } |
|
5
|
|
|
|
|
38
|
|
19
|
2
|
|
|
|
|
11
|
return \@_; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
elsif (ref $_[0] eq 'HASH') { |
22
|
1
|
|
|
|
|
2
|
foreach my $k (keys %{ $_[0] }) { |
|
1
|
|
|
|
|
5
|
|
23
|
5
|
|
|
|
|
25
|
(my $nk = $k) =~ s{$t}{}g; |
24
|
5
|
100
|
|
|
|
12
|
if (defined $_[0]->{$k}) { |
25
|
4
|
|
|
|
|
24
|
($_[0]->{$nk} = $_[0]->{$k}) =~ s{$t}{}g; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
else { |
28
|
1
|
|
|
|
|
3
|
$_[0]->{$nk} = undef; |
29
|
|
|
|
|
|
|
} |
30
|
5
|
100
|
|
|
|
18
|
delete $_[0]->{$k} unless $k eq $nk; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
else { |
34
|
12
|
50
|
|
|
|
42
|
for (@_ ? @_ : $_) { s{$t}{}g if defined $_ } |
|
15
|
100
|
|
|
|
139
|
|
35
|
|
|
|
|
|
|
} |
36
|
13
|
100
|
|
|
|
80
|
return wantarray ? @_ : $_[0]; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
else { |
39
|
21
|
100
|
|
|
|
99
|
if (ref $_[0] eq 'ARRAY') { |
|
|
100
|
|
|
|
|
|
40
|
2
|
100
|
|
|
|
4
|
for (@{ $_[0] }) { s{$t}{}g if defined $_ } |
|
2
|
|
|
|
|
5
|
|
|
5
|
|
|
|
|
41
|
|
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
elsif (ref $_[0] eq 'HASH') { |
43
|
1
|
|
|
|
|
2
|
foreach my $k (keys %{ $_[0] }) { |
|
1
|
|
|
|
|
4
|
|
44
|
5
|
|
|
|
|
19
|
(my $nk = $k) =~ s{$t}{}g; |
45
|
5
|
100
|
|
|
|
12
|
if (defined $_[0]->{$k}) { |
46
|
4
|
|
|
|
|
16
|
($_[0]->{$nk} = $_[0]->{$k}) =~ s{$t}{}g; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
else { |
49
|
1
|
|
|
|
|
2
|
$_[0]->{$nk} = undef; |
50
|
|
|
|
|
|
|
} |
51
|
5
|
50
|
|
|
|
16
|
delete $_[0]->{$k} unless $k eq $nk |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
else { |
55
|
18
|
100
|
|
|
|
292
|
for (@_ ? @_ : $_) { s{$t}{}g if defined $_ } |
|
30
|
100
|
|
|
|
273
|
|
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
1; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=pod |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=encoding utf-8 |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 NAME |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
String::Trim - trim whitespace from your strings |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 VERSION |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
version 0.005 |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 SYNOPSIS |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
use String::Trim; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
print "Do it? "; |
82
|
|
|
|
|
|
|
trim(my $response = <>); |
83
|
|
|
|
|
|
|
print "'$response'\n"; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 DESCRIPTION |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
C trims whitespace off your strings. L trims only C<$/> (typically, |
88
|
|
|
|
|
|
|
that's newline), but C will trim all leading and trailing whitespace. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 FUNCTIONS |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head2 trim |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Returns the string with all leading and trailing whitespace removed. Trimming |
95
|
|
|
|
|
|
|
undef gives you undef. Alternatively, you can trim in-place. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my $var = ' my string '; |
98
|
|
|
|
|
|
|
my $trimmed = trim($var); |
99
|
|
|
|
|
|
|
# OR |
100
|
|
|
|
|
|
|
trim($var); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
C also knows how to trim an array or arrayref: |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
my @to_trim = (' one ', ' two ', ' three '); |
105
|
|
|
|
|
|
|
my @trimmed = trim(@to_trim); |
106
|
|
|
|
|
|
|
# OR |
107
|
|
|
|
|
|
|
trim(@to_trim); |
108
|
|
|
|
|
|
|
# OR |
109
|
|
|
|
|
|
|
my $trimmed = trim(\@to_trim); |
110
|
|
|
|
|
|
|
# OR |
111
|
|
|
|
|
|
|
trim(\@to_trim); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 RATIONALE |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
C is often used by beginners, who may not understand how to spin their own. While |
116
|
|
|
|
|
|
|
L does have a C function, it depends on L, which |
117
|
|
|
|
|
|
|
depends on L, which fails the test suite, and doesn't appear to be maintained. |
118
|
|
|
|
|
|
|
This module installs, is actively maintained, and has no non-core dependencies. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Other options include L and L (which is implemented in XS, |
121
|
|
|
|
|
|
|
and is therefore likely to be very fast, but requires a C compiler). |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 AVAILABILITY |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
The latest version of this module is available from the Comprehensive Perl |
126
|
|
|
|
|
|
|
Archive Network (CPAN). Visit L to find a CPAN |
127
|
|
|
|
|
|
|
site near you, or see L. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
The development version lives at L |
130
|
|
|
|
|
|
|
and may be cloned from L. |
131
|
|
|
|
|
|
|
Instead of sending patches, please fork this project using the standard |
132
|
|
|
|
|
|
|
git and github infrastructure. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 SOURCE |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
The development version is on github at L |
137
|
|
|
|
|
|
|
and may be cloned from L |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 BUGS AND LIMITATIONS |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
No bugs have been reported. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Please report any bugs or feature requests through the web interface at |
144
|
|
|
|
|
|
|
L. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 CREDITS |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
This module was inspired by code at L written |
149
|
|
|
|
|
|
|
by japhy (Jeff Pinyan), dragonchild, and Aristotle. This Perl module was written by Mike |
150
|
|
|
|
|
|
|
Doherty. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 AUTHORS |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=over 4 |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item * |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Mike Doherty |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item * |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Jeff Pinyan |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item * |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Rob Kinyon |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item * |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Αριστοτέλης Παγκαλτζής (Aristotle Pagaltzis) |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=back |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
This software is copyright (c) 2010 by Mike Doherty. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
179
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=cut |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
__END__ |