| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
|
2
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
|
3
|
|
|
|
|
|
|
# Trim various things by removing leading and trailing whitespace. |
|
4
|
|
|
|
|
|
|
# Philip R Brenan at appaapps dot com, Appa Apps Ltd Inc., 2021 |
|
5
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
|
6
|
|
|
|
|
|
|
# podDocumentation |
|
7
|
|
|
|
|
|
|
package Trim; |
|
8
|
|
|
|
|
|
|
our $VERSION = "20210401"; |
|
9
|
1
|
|
|
1
|
|
627
|
use warnings FATAL => qw(all); |
|
|
1
|
|
|
|
|
8
|
|
|
|
1
|
|
|
|
|
38
|
|
|
10
|
1
|
|
|
1
|
|
5
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
24
|
|
|
11
|
1
|
|
|
1
|
|
6
|
use feature qw(say current_sub); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
1101
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
#D1 Trim # Trim strings, arrays, hashes in situ and clones thereof. |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub trim(@) # Trim somethings. |
|
16
|
68
|
|
|
68
|
1
|
213
|
{my (@things) = @_; # Things to be trimmed |
|
17
|
|
|
|
|
|
|
|
|
18
|
68
|
100
|
|
|
|
134
|
if (@_ == 0) # Trim $_ as no argument specified |
|
19
|
7
|
100
|
|
|
|
20
|
{if (!defined(wantarray)) # Void context - trim in place |
|
20
|
4
|
100
|
|
|
|
21
|
{if (!ref) # Scalar string in $_ in void context |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
21
|
1
|
|
|
|
|
21
|
{s/\A\s+|\s+\Z//gs; |
|
22
|
|
|
|
|
|
|
} |
|
23
|
|
|
|
|
|
|
elsif (ref eq q(ARRAY)) # Array referenced from $_ in void context |
|
24
|
2
|
|
|
|
|
9
|
{for my $i(keys @$_) |
|
25
|
5
|
|
|
|
|
17
|
{$$_[$i] = __SUB__->($$_[$i]); |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
} |
|
28
|
|
|
|
|
|
|
elsif (ref eq q(HASH)) # Hash referenced from $_ in void context |
|
29
|
1
|
|
|
|
|
5
|
{for my $k(keys %$_) |
|
30
|
1
|
|
|
|
|
4
|
{$$_{$k} = __SUB__->($$_{$k}); |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
} |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
else # Not void context - caller wants something back |
|
35
|
3
|
100
|
|
|
|
18
|
{if (!ref) # Scalar string in $_ in non void context |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
36
|
1
|
|
|
|
|
4
|
{return __SUB__->($_); |
|
37
|
|
|
|
|
|
|
} |
|
38
|
|
|
|
|
|
|
elsif (ref eq q(ARRAY)) # Array referenced from $_ in non void context |
|
39
|
1
|
|
|
|
|
5
|
{return [__SUB__->(@$_)]; |
|
40
|
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
elsif (ref eq q(HASH)) # Hash referenced from $_ in non void context |
|
42
|
1
|
|
|
|
|
6
|
{return {__SUB__->(%$_)}; |
|
43
|
|
|
|
|
|
|
} |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
} |
|
46
|
|
|
|
|
|
|
else # Arguments specified |
|
47
|
61
|
100
|
|
|
|
105
|
{if (!defined(wantarray)) # Void context - trim in place |
|
48
|
6
|
|
|
|
|
16
|
{for my $i(keys @_) |
|
49
|
8
|
100
|
|
|
|
34
|
{if (!ref $_[$i]) # Trim scalar in void context |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
50
|
5
|
|
|
|
|
11
|
{$_[$i] = __SUB__->($_[$i]); |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
elsif (ref($_[$i]) eq q(ARRAY)) # Trim array reference in void context |
|
53
|
2
|
|
|
|
|
9
|
{for my $j(keys $_[$i]->@*) |
|
54
|
5
|
|
|
|
|
12
|
{$_[$i][$j] = __SUB__->($_[$i][$j]); |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
elsif (ref($_[$i]) eq q(HASH)) # Trim hash reference in void context |
|
58
|
1
|
|
|
|
|
5
|
{for my $k(keys $_[$i]->%*) |
|
59
|
1
|
|
|
|
|
4
|
{$_[$i]{$k} = __SUB__->($_[$i]{$k}); |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
else # Want something back |
|
65
|
55
|
|
|
|
|
71
|
{my @r; |
|
66
|
55
|
|
|
|
|
96
|
for my $i(keys @_) |
|
67
|
66
|
100
|
|
|
|
127
|
{if ( !ref $_[$i]) # Trim clone of scalar and return cloned value |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
68
|
55
|
|
|
|
|
234
|
{push @r, $_[$i] =~ s/\A\s+|\s+\Z//gsr; |
|
69
|
55
|
100
|
|
|
|
339
|
return $r[0] if @_ == 1; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
elsif (ref($_[$i]) eq q(ARRAY)) # Trim clone of array reference and return cloned value |
|
72
|
3
|
|
|
|
|
8
|
{my @a; |
|
73
|
3
|
|
|
|
|
10
|
for my $j(keys $_[$i]->@*) |
|
74
|
7
|
|
|
|
|
17
|
{push @a, __SUB__->($_[$i][$j]); |
|
75
|
|
|
|
|
|
|
} |
|
76
|
3
|
|
|
|
|
10
|
push @r, [@a]; |
|
77
|
3
|
100
|
|
|
|
44
|
return $r[0] if @_ == 1; |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
elsif (ref($_[$i]) eq q(HASH)) # Trim clone of hash reference and return cloned value |
|
80
|
8
|
|
|
|
|
11
|
{my @a; |
|
81
|
8
|
|
|
|
|
23
|
for my $k(keys $_[$i]->%*) |
|
82
|
8
|
|
|
|
|
54
|
{push @a, (__SUB__->($k), __SUB__->($_[$i]{$k})); |
|
83
|
|
|
|
|
|
|
} |
|
84
|
8
|
|
|
|
|
22
|
push @r, {@a}; |
|
85
|
8
|
100
|
|
|
|
84
|
return $r[0] if @_ == 1; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
} |
|
88
|
9
|
50
|
|
|
|
189
|
return wantarray ? @r : @_ == $r[-1]; # Return array if requested else last element trimmed |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
#d |
|
94
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
|
95
|
|
|
|
|
|
|
# Export - eeee |
|
96
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
|
97
|
|
|
|
|
|
|
|
|
98
|
1
|
|
|
1
|
|
10
|
use Exporter qw(import); |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
60
|
|
|
99
|
|
|
|
|
|
|
|
|
100
|
1
|
|
|
1
|
|
6
|
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
324
|
|
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
|
103
|
|
|
|
|
|
|
@EXPORT = qw(trim); |
|
104
|
|
|
|
|
|
|
@EXPORT_OK = qw(); |
|
105
|
|
|
|
|
|
|
%EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]); |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# podDocumentation |
|
108
|
|
|
|
|
|
|
=pod |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=encoding utf-8 |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 Name |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Trim - Trim various things by removing leading and trailing whitespace |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 Synopsis |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Trim nested structures in situ referenced from $_: |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
{$_ = [" a ", " b ", {" c " => {" d\n ", " e "}}]; |
|
121
|
|
|
|
|
|
|
trim; |
|
122
|
|
|
|
|
|
|
is_deeply $_, ["a", "b", { c => { d => "e" } }]; |
|
123
|
|
|
|
|
|
|
} |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Trim nested structures in situ: |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
{my $a = [" a ", " b ", {" c " => {" d\n ", " e "}}]; |
|
128
|
|
|
|
|
|
|
trim $a; |
|
129
|
|
|
|
|
|
|
is_deeply $a, ["a", "b", { c => { d => "e" } }]; |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Trim cloned nested structures: |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
{my $a = [" a ", " b ", {" c " => {" d\n ", " e "}}]; |
|
135
|
|
|
|
|
|
|
my $b = trim $a; |
|
136
|
|
|
|
|
|
|
is_deeply $b, ["a", "b", { c => { d => "e" } }]; |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 Description |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Trim various things by removing leading and trailing whitespace |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Version "20210401". |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
The following sections describe the methods in each functional area of this |
|
148
|
|
|
|
|
|
|
module. For an alphabetic listing of all methods by name see L. |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 Trim |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Trim strings, arrays, hashes in situ and clones thereof. |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head2 trim(@things) |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Trim somethings. |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Parameter Description |
|
161
|
|
|
|
|
|
|
1 @things Things to be trimmed |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
B |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
$_ = [" a ", " b ", {" c " => {" d ", " e "}}]; # Trim nested structures in situ referenced from $_ |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
trim; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲 |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
is_deeply $_, ["a", "b", { c => { d => "e" } }]; |
|
171
|
|
|
|
|
|
|
} |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
{my $a = [" a ", " b ", {" c " => {" d ", " e "}}]; # Trim nested structures in situ |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
trim $a; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲 |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
is_deeply $a, ["a", "b", { c => { d => "e" } }]; |
|
178
|
|
|
|
|
|
|
} |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
{my $a = [" a ", " b ", {" c " => {" d ", " e "}}]; # Trim cloned nested structures |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
my $b = trim $a; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲 |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
is_deeply $b, ["a", "b", { c => { d => "e" } }]; |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 Index |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
1 L - Trim somethings. |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head1 Installation |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
This module is written in 100% Pure Perl and, thus, it is easy to read, |
|
196
|
|
|
|
|
|
|
comprehend, use, modify and install via B: |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sudo cpan install Trim |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 Author |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
L |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
L |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 Copyright |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Copyright (c) 2016-2021 Philip R Brenan. |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
This module is free software. It may be used, redistributed and/or modified |
|
211
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=cut |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
# Tests and documentation |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
sub test |
|
220
|
1
|
|
|
1
|
0
|
8
|
{my $p = __PACKAGE__; |
|
221
|
1
|
|
|
|
|
11
|
binmode($_, ":utf8") for *STDOUT, *STDERR; |
|
222
|
1
|
50
|
|
|
|
70
|
return if eval "eof(${p}::DATA)"; |
|
223
|
1
|
|
|
|
|
56
|
my $s = eval "join('', <${p}::DATA>)"; |
|
224
|
1
|
50
|
|
|
|
10
|
$@ and die $@; |
|
225
|
1
|
|
|
1
|
|
693
|
eval $s; |
|
|
1
|
|
|
1
|
|
1593
|
|
|
|
1
|
|
|
|
|
5
|
|
|
|
1
|
|
|
|
|
1017
|
|
|
|
1
|
|
|
|
|
72299
|
|
|
|
1
|
|
|
|
|
14
|
|
|
|
1
|
|
|
|
|
85
|
|
|
226
|
1
|
50
|
|
|
|
1046
|
$@ and die $@; |
|
227
|
1
|
|
|
|
|
144
|
1 |
|
228
|
|
|
|
|
|
|
} |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
test unless caller; |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
1; |
|
233
|
|
|
|
|
|
|
# podDocumentation |
|
234
|
|
|
|
|
|
|
__DATA__ |