line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MiscUtils;
|
2
|
|
|
|
|
|
|
$VERSION = '1.0.0';
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
57025
|
use strict;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2891
|
|
5
|
|
|
|
|
|
|
require Exporter;
|
6
|
|
|
|
|
|
|
require Carp;
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
9
|
|
|
|
|
|
|
our @EXPORT = qw(rindent indent mkdirs swap_dirs filter_text debug);
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
MiscUtils - Miscellanous Utitlies. Provided in hopes that you will add your own functions.
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use MiscUtils;
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
print rindent(4).'I am 4 tabs over!';
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
mkdirs('/xtra/shared/mp3s/by/author/m/mo/mono/life_in_mono.mp3');
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# hypothetical - see discussion below
|
24
|
|
|
|
|
|
|
this_useful_function_I_wrote_and_use_constantly ('I added it in myself');
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
MiscUtils is a collection of utility functions I found myself using
|
29
|
|
|
|
|
|
|
in alot of my scripts. Then I thought it would be better to just
|
30
|
|
|
|
|
|
|
throw them all into one huge unrelated, incoherant module. Then
|
31
|
|
|
|
|
|
|
I thought it would be good to share with the world. Then I thought
|
32
|
|
|
|
|
|
|
the world would benifit even more if they threw in thier own
|
33
|
|
|
|
|
|
|
functions they constantly use.
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head2 DISCLAIMER
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
This module is mostly provided as a means of making quick hacks quicker. For a serious
|
38
|
|
|
|
|
|
|
or distributed script you should not rely on or require anyone to have
|
39
|
|
|
|
|
|
|
this module. To get the functions out of this module into your
|
40
|
|
|
|
|
|
|
serious or distributed scripts, just cut and paste directly into
|
41
|
|
|
|
|
|
|
your script or module(s). The module is nothing special, so don't be afraid to hack it up.
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 PROVIDED FUNCTIONS
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head2 rindent
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Returns x number of tabs.
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Arguments
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=over
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item 1
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
The number of spaces to indent.
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=back
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Returns: (string) argument number of tabs
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Example:
|
62
|
|
|
|
|
|
|
print rindent(4).'I am 4 tabs over!';
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=cut
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub rindent {
|
67
|
0
|
|
|
0
|
1
|
|
my $ident = shift();
|
68
|
0
|
|
|
|
|
|
my $tmp = '';
|
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
while ($ident--) {
|
71
|
0
|
|
|
|
|
|
$tmp .= "\t";
|
72
|
|
|
|
|
|
|
}
|
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
return $tmp;
|
75
|
|
|
|
|
|
|
}
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 indent
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Prints onto a filehandle or stdout x number of tabs.
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Arguments
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=over
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=item 1
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
The number of spaces to indent.
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=item 2
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
A reference to a filehandle [optional] [default:STDOUT]
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=back
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Returns: nothing
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Example:
|
98
|
|
|
|
|
|
|
indent(4);
|
99
|
|
|
|
|
|
|
print "I am 4 tabs over!\n";
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub indent {
|
104
|
0
|
|
|
0
|
1
|
|
my ($indent, $fh) = @_;
|
105
|
|
|
|
|
|
|
|
106
|
0
|
0
|
|
|
|
|
if ($fh) { print $fh rindent ($indent); }
|
|
0
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
else { print rindent ($indent); }
|
108
|
|
|
|
|
|
|
}
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head2 mkdirs
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Makes the directories specified in the path.
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Arguments
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=over
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=item 1
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
The full file/path name to be made if it does not exist.
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=back
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Returns: 1 upon success, 0 upon failure.
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Example:
|
127
|
|
|
|
|
|
|
if (mkdirs('/xtra/shared/mp3s/by/author/m/mo/mono/life_in_mono.mp3')) {
|
128
|
|
|
|
|
|
|
# /xtra/shared/mp3s/by/author/m/mo/mono/ will definitaly exist.
|
129
|
|
|
|
|
|
|
}
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=cut
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub mkdirs {
|
134
|
0
|
|
|
0
|
1
|
|
my $full_path = shift();
|
135
|
0
|
0
|
|
|
|
|
return if (-d $full_path);
|
136
|
0
|
|
|
|
|
|
$full_path =~ s-^((?:/)?.+)/.+?$-$1/-;
|
137
|
0
|
|
|
|
|
|
my @all_dirs = split(/\//, $full_path);
|
138
|
0
|
|
|
|
|
|
my ($dir, $tmp_dir) = ();
|
139
|
0
|
|
|
|
|
|
foreach $dir (@all_dirs) {
|
140
|
0
|
|
|
|
|
|
$tmp_dir .= "$dir/";
|
141
|
0
|
0
|
|
|
|
|
if (!-e $tmp_dir) {
|
142
|
0
|
0
|
|
|
|
|
if (!mkdir($tmp_dir)) {
|
143
|
0
|
|
|
|
|
|
return 0;
|
144
|
|
|
|
|
|
|
}
|
145
|
|
|
|
|
|
|
}
|
146
|
|
|
|
|
|
|
}
|
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
return 1;
|
149
|
|
|
|
|
|
|
}
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head2 swap_dirs
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Simplify relative path, path blending.
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Arguments
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=over
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item 1
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
The full file/path name
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item 2
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
The first part of the path that will be swapped.
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item 3
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
The first part of the path that you want to swap with.
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=back
|
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Returns: The new path.
|
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Example:
|
176
|
|
|
|
|
|
|
print swap_dirs('/publicwww/oldsite/index.php', '/publicwww/', 'http://lackluster.tzo.com:1024/');
|
177
|
|
|
|
|
|
|
# prints http://lackluster.tzo.com:1024/oldsite/index.php
|
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=cut
|
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
sub swap_dirs {
|
182
|
0
|
|
|
0
|
1
|
|
my ($path, $from, $to) = @_;
|
183
|
0
|
|
|
|
|
|
$path =~ s/^$from/$to/i;
|
184
|
0
|
|
|
|
|
|
return ($path);
|
185
|
|
|
|
|
|
|
}
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head2 debug
|
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Debug (pretty print) array ref, hash ref, or scalar. Not as cool as L
|
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Arguments
|
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=over
|
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=item 1
|
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
The item to be debugged. Arrays and Hashes must be in reference form.
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=item 2
|
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Variable/Output name.
|
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=item 3
|
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Indentation level [optional|internal]
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=back
|
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Returns: nothing
|
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Example:
|
212
|
|
|
|
|
|
|
my @words = qw(a ab abc abcd);
|
213
|
|
|
|
|
|
|
debug (\@words, 'my words');
|
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=cut
|
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub debug {
|
218
|
0
|
|
|
0
|
1
|
|
my ($whatnot, $name, $indent) = @_;
|
219
|
|
|
|
|
|
|
|
220
|
0
|
0
|
|
|
|
|
$name = 'VARIABLE' if (!$name);
|
221
|
0
|
0
|
|
|
|
|
$whatnot = '(empty)' if (!$whatnot);
|
222
|
|
|
|
|
|
|
|
223
|
0
|
0
|
|
|
|
|
if (ref($whatnot) eq 'ARRAY') {
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
224
|
0
|
|
|
|
|
|
$indent++;
|
225
|
0
|
|
|
|
|
|
for (my $i = 0; $i < scalar(@{ $whatnot }); $i++) {
|
|
0
|
|
|
|
|
|
|
226
|
0
|
|
|
|
|
|
debug ($whatnot->[$i], "${name}\[$i\]", $indent);
|
227
|
|
|
|
|
|
|
}
|
228
|
0
|
|
|
|
|
|
$indent--;
|
229
|
|
|
|
|
|
|
}
|
230
|
|
|
|
|
|
|
elsif (ref($whatnot) eq 'HASH') {
|
231
|
0
|
|
|
|
|
|
$indent++;
|
232
|
0
|
|
|
|
|
|
foreach my $item (keys %{ $whatnot }) {
|
|
0
|
|
|
|
|
|
|
233
|
0
|
|
|
|
|
|
debug ($whatnot->{$item}, "${name}\{$item\}", $indent);
|
234
|
|
|
|
|
|
|
}
|
235
|
0
|
|
|
|
|
|
$indent--;
|
236
|
|
|
|
|
|
|
}
|
237
|
|
|
|
|
|
|
elsif (ref($whatnot) eq 'SCALAR') {
|
238
|
0
|
|
|
|
|
|
debug ($$whatnot, $name, $indent);
|
239
|
|
|
|
|
|
|
}
|
240
|
|
|
|
|
|
|
elsif (ref($whatnot) eq 'CODE') {
|
241
|
0
|
|
|
|
|
|
debug ('a code reference', $name, $indent);
|
242
|
|
|
|
|
|
|
}
|
243
|
|
|
|
|
|
|
else {
|
244
|
0
|
|
|
|
|
|
print ("debug: ");
|
245
|
0
|
|
|
|
|
|
while ($indent--) { print "\t"; }
|
|
0
|
|
|
|
|
|
|
246
|
0
|
|
|
|
|
|
print ("$name is $whatnot\n");
|
247
|
|
|
|
|
|
|
}
|
248
|
|
|
|
|
|
|
}
|
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=head2 filter_text
|
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
Transforms non-XML-data elements to useful stoarge.
|
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
Arguments
|
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=over
|
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=item 1
|
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
Text to transform.
|
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=back
|
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
Returns: Transformed Text.
|
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
Example:
|
267
|
|
|
|
|
|
|
print $some_var_with_smart_quotes_and_punctuation;
|
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=cut
|
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
sub filter_text {
|
272
|
0
|
|
|
0
|
1
|
|
my $dirty_text = shift();
|
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
# trim
|
275
|
0
|
|
|
|
|
|
$dirty_text =~ s/^ +//; $dirty_text =~ s/ +$//;
|
|
0
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
# "smart" quotes X{
|
278
|
0
|
|
|
|
|
|
$dirty_text =~ tr/\x93\x94/"/; $dirty_text =~ tr/\x92/'/;
|
|
0
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
# convert special chars
|
281
|
0
|
|
|
|
|
|
$dirty_text =~ s/&/&/g;
|
282
|
0
|
|
|
|
|
|
$dirty_text =~ s/</g; $dirty_text =~ s/>/>/g;
|
|
0
|
|
|
|
|
|
|
283
|
0
|
|
|
|
|
|
$dirty_text =~ s/"/"/g; $dirty_text =~ s/'/'/g;
|
|
0
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
|
285
|
0
|
|
|
|
|
|
return $dirty_text;
|
286
|
|
|
|
|
|
|
}
|
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
1;
|
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
__END__
|