line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Goo::FileUtilities; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
############################################################################### |
4
|
|
|
|
|
|
|
# trexy.com - handle files |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Copyright Nigel Hamilton 2002 |
7
|
|
|
|
|
|
|
# All Rights Reserved |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# Author: Nigel Hamilton |
10
|
|
|
|
|
|
|
# Filename: Goo::FileUtilities.pm |
11
|
|
|
|
|
|
|
# Description: General file handling utilities |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
# Date Change |
15
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
16
|
|
|
|
|
|
|
# 17/06/2002 Version 1 |
17
|
|
|
|
|
|
|
# 07/07/2004 Added simple file writing method |
18
|
|
|
|
|
|
|
# 10/03/2005 Added mtime on checker |
19
|
|
|
|
|
|
|
# 01/07/2005 Added getPath - smarter regex handling |
20
|
|
|
|
|
|
|
# 01/07/2005 Added getSuffix |
21
|
|
|
|
|
|
|
# 17/10/2005 Added method: getCWD |
22
|
|
|
|
|
|
|
# 02/12/2005 Added getLastLines - tail replacement |
23
|
|
|
|
|
|
|
# |
24
|
|
|
|
|
|
|
############################################################################### |
25
|
|
|
|
|
|
|
|
26
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
27
|
|
|
|
|
|
|
|
28
|
1
|
|
|
1
|
|
6
|
use Cwd; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
60
|
|
29
|
1
|
|
|
1
|
|
5
|
use File::Spec; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
30
|
1
|
|
|
1
|
|
851
|
use File::stat; |
|
1
|
|
|
|
|
7571
|
|
|
1
|
|
|
|
|
7
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
############################################################################### |
33
|
|
|
|
|
|
|
# |
34
|
|
|
|
|
|
|
# get_mtime - get file modification time |
35
|
|
|
|
|
|
|
# |
36
|
|
|
|
|
|
|
############################################################################### |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub get_mtime { |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
0
|
1
|
0
|
my ($filename) = @_; |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
0
|
my $file_info = stat($filename); |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
0
|
return $file_info->mtime(); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
############################################################################### |
49
|
|
|
|
|
|
|
# |
50
|
|
|
|
|
|
|
# get_file_hash - return a hash of files and their contents |
51
|
|
|
|
|
|
|
# |
52
|
|
|
|
|
|
|
############################################################################### |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub get_file_hash { |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
0
|
1
|
0
|
my ($directory) = @_; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
0
|
my $filehash = {}; |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
0
|
my @files = get_file_list($directory); |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
0
|
foreach my $file (@files) { |
63
|
0
|
|
|
|
|
0
|
$filehash->{$file} = get_file_as_string($file); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
0
|
return $filehash; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
############################################################################### |
71
|
|
|
|
|
|
|
# |
72
|
|
|
|
|
|
|
# get_short_file_list - filenames list only |
73
|
|
|
|
|
|
|
# |
74
|
|
|
|
|
|
|
############################################################################### |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub get_short_file_list { |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
0
|
1
|
0
|
my ($directory) = @_; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
0
|
return map { $_ =~ s!^.*/!! } get_file_list($directory); |
|
0
|
|
|
|
|
0
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
############################################################################### |
85
|
|
|
|
|
|
|
# |
86
|
|
|
|
|
|
|
# get_file_list - return a list of file based on a directory glob |
87
|
|
|
|
|
|
|
# |
88
|
|
|
|
|
|
|
############################################################################### |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub get_file_list { |
91
|
|
|
|
|
|
|
|
92
|
1
|
|
|
1
|
1
|
12
|
my $directory = shift; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# restore line mode |
95
|
1
|
|
|
|
|
5
|
$/ = "\n"; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# read in all files from directory like with `ls $directory` |
98
|
1
|
|
|
|
|
13
|
opendir THISDIR, $directory; |
99
|
1
|
|
|
|
|
6
|
my @newfiles = sort { lc $a cmp lc $b} |
|
0
|
|
|
|
|
0
|
|
100
|
|
|
|
|
|
|
grep !/^\./, readdir THISDIR; |
101
|
1
|
|
|
|
|
3
|
closedir THISDIR; |
102
|
|
|
|
|
|
|
|
103
|
1
|
|
|
|
|
33
|
return @newfiles; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
############################################################################### |
108
|
|
|
|
|
|
|
# |
109
|
|
|
|
|
|
|
# get_file_as_string_ref - grab a file as a string |
110
|
|
|
|
|
|
|
# |
111
|
|
|
|
|
|
|
############################################################################### |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub get_file_as_string_ref { |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
0
|
1
|
|
my ($filename) = @_; |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
local $/; # put PERL into slurp mode |
118
|
|
|
|
|
|
|
|
119
|
0
|
0
|
|
|
|
|
open(FILE, "< $filename") |
120
|
|
|
|
|
|
|
or die("[" . caller() . "] Can't open file for reading: $filename\n"); |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
my $filecontents = <FILE>; # slurp int entire file |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
close(FILE); |
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
return \$filecontents; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
############################################################################### |
131
|
|
|
|
|
|
|
# |
132
|
|
|
|
|
|
|
# get_file_as_string - grab a file as a string |
133
|
|
|
|
|
|
|
# |
134
|
|
|
|
|
|
|
############################################################################### |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub get_file_as_string { |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
0
|
1
|
|
my ($filename) = @_; |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
local $/; # put PERL into slurp mode |
141
|
|
|
|
|
|
|
|
142
|
0
|
0
|
|
|
|
|
open(FILE, "< $filename") |
143
|
|
|
|
|
|
|
or die("[" . caller() . "] Can't open file for reading: $filename\n"); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# suggested by Damian Conway's Best Practices |
146
|
0
|
|
|
|
|
|
my $filecontents = do { local $/; <FILE>; }; # slurp int entire file |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
close(FILE); |
149
|
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
|
return $filecontents; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
############################################################################### |
155
|
|
|
|
|
|
|
# |
156
|
|
|
|
|
|
|
# write_file - write a file |
157
|
|
|
|
|
|
|
# |
158
|
|
|
|
|
|
|
############################################################################### |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub write_file { |
161
|
|
|
|
|
|
|
|
162
|
0
|
|
|
0
|
1
|
|
my ($filename, $string) = @_; |
163
|
|
|
|
|
|
|
|
164
|
0
|
0
|
|
|
|
|
open(FILE, "> $filename") |
165
|
|
|
|
|
|
|
or die("[" . caller() . "] Can't open file for writing: $filename\n"); |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
print FILE $string; |
168
|
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
|
close(FILE); |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
############################################################################### |
174
|
|
|
|
|
|
|
# |
175
|
|
|
|
|
|
|
# get_file_as_lines - grab a file as an array of lines |
176
|
|
|
|
|
|
|
# |
177
|
|
|
|
|
|
|
############################################################################### |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub get_file_as_lines { |
180
|
|
|
|
|
|
|
|
181
|
0
|
|
|
0
|
1
|
|
my ($filename) = @_; |
182
|
|
|
|
|
|
|
|
183
|
0
|
0
|
|
|
|
|
open(FILE, "< $filename") |
184
|
|
|
|
|
|
|
or die("[" . caller() . "] Can't open file for reading: $filename\n)"); |
185
|
|
|
|
|
|
|
|
186
|
0
|
|
|
|
|
|
my @lines = <FILE>; # slurp int entire file |
187
|
|
|
|
|
|
|
|
188
|
0
|
|
|
|
|
|
close(FILE); |
189
|
|
|
|
|
|
|
|
190
|
0
|
|
|
|
|
|
return @lines; |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
############################################################################### |
195
|
|
|
|
|
|
|
# |
196
|
|
|
|
|
|
|
# write_lines_as_file - write an array of lines to a file |
197
|
|
|
|
|
|
|
# |
198
|
|
|
|
|
|
|
############################################################################### |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub write_lines_as_file { |
201
|
|
|
|
|
|
|
|
202
|
0
|
|
|
0
|
1
|
|
my ($filename, @lines) = @_; |
203
|
|
|
|
|
|
|
|
204
|
0
|
0
|
|
|
|
|
open(FILE, "> $filename") |
205
|
|
|
|
|
|
|
or die("[" . caller() . "] Can't open file for writing: $filename\n)"); |
206
|
|
|
|
|
|
|
|
207
|
0
|
|
|
|
|
|
foreach my $line (@lines) { |
208
|
|
|
|
|
|
|
|
209
|
0
|
0
|
|
|
|
|
if ($line !~ m/\n$/) { |
210
|
0
|
|
|
|
|
|
$line .= "\n"; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
0
|
|
|
|
|
|
print FILE $line; |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
0
|
|
|
|
|
|
close(FILE); |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
############################################################################### |
222
|
|
|
|
|
|
|
# |
223
|
|
|
|
|
|
|
# get_suffix - return the suffix of this filename |
224
|
|
|
|
|
|
|
# |
225
|
|
|
|
|
|
|
############################################################################### |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
sub get_suffix { |
228
|
|
|
|
|
|
|
|
229
|
0
|
|
|
0
|
1
|
|
my ($filename) = @_; |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
# strip trailing whitespace |
232
|
0
|
|
|
|
|
|
$filename =~ s/\s+$//; |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
# grab the suffix |
235
|
0
|
|
|
|
|
|
$filename =~ m/.*\.(.*)$/; |
236
|
|
|
|
|
|
|
|
237
|
0
|
|
|
|
|
|
return $1; |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
############################################################################### |
242
|
|
|
|
|
|
|
# |
243
|
|
|
|
|
|
|
# get_path - return the path portion of the filename |
244
|
|
|
|
|
|
|
# |
245
|
|
|
|
|
|
|
############################################################################### |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
sub get_path { |
248
|
|
|
|
|
|
|
|
249
|
0
|
|
|
0
|
1
|
|
my ($filename) = @_; |
250
|
|
|
|
|
|
|
|
251
|
0
|
|
|
|
|
|
my ($volume, $directories, $file) = File::Spec->splitpath($filename); |
252
|
|
|
|
|
|
|
|
253
|
0
|
|
|
|
|
|
my $path = File::Spec->catpath($volume, $directories); |
254
|
|
|
|
|
|
|
|
255
|
0
|
|
|
|
|
|
return $path; |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
############################################################################### |
260
|
|
|
|
|
|
|
# |
261
|
|
|
|
|
|
|
# slurp - synonym for get_file_as_string |
262
|
|
|
|
|
|
|
# |
263
|
|
|
|
|
|
|
############################################################################### |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
sub slurp { |
266
|
|
|
|
|
|
|
|
267
|
0
|
|
|
0
|
1
|
|
my ($filename) = @_; |
268
|
|
|
|
|
|
|
|
269
|
0
|
|
|
|
|
|
return get_file_as_string($filename); |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
############################################################################### |
274
|
|
|
|
|
|
|
# |
275
|
|
|
|
|
|
|
# get_cwd - return the current working directory |
276
|
|
|
|
|
|
|
# |
277
|
|
|
|
|
|
|
############################################################################### |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
sub get_cwd { |
280
|
|
|
|
|
|
|
|
281
|
0
|
|
|
0
|
1
|
|
return getcwd(); |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
############################################################################### |
286
|
|
|
|
|
|
|
# |
287
|
|
|
|
|
|
|
# get_last_lines - return the n last lines from a file |
288
|
|
|
|
|
|
|
# |
289
|
|
|
|
|
|
|
############################################################################### |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
sub get_last_lines { |
292
|
|
|
|
|
|
|
|
293
|
0
|
|
|
0
|
1
|
|
my ($filename, $n) = @_; |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
# set default number of lines |
296
|
0
|
|
0
|
|
|
|
$n ||= 10; |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
###TODO### could be rewriten to only hold $n lines at one time |
299
|
|
|
|
|
|
|
|
300
|
0
|
|
|
|
|
|
my @lines = get_file_as_lines($filename); |
301
|
|
|
|
|
|
|
|
302
|
0
|
|
|
|
|
|
return split(@lines, -$n, $n); |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
} |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
1; |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
__END__ |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=head1 NAME |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
Goo::FileUtilities - General file handling utilities |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=head1 SYNOPSIS |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
use Goo::FileUtilities; |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=head1 DESCRIPTION |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
File utility functions. |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=head1 METHODS |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
=over |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
=item get_mtime |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
get file modification time |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
=item get_file_hash |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
return a hash of files and their contents |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
=item get_short_file_list |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
return a list of filenames in a directory |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
=item get_file_list |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
return a list of file based on a directory glob |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
=item get_file_as_string_ref |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
return a string_ref to the contents of a file |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
=item get_file_as_string |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
return the file contents as a string |
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
=item write_file |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
write a file |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
=item get_file_as_lines |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
return the file contents as an array of lines |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
=item write_lines_as_file |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
write an array of lines to a file |
362
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
=item get_suffix |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
return the suffix of this filename |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
=item get_path |
368
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
return the path portion of the filename |
370
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
=item slurp |
372
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
Perl6 synonym for get_file_as_string |
374
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
=item get_cwd |
376
|
|
|
|
|
|
|
|
377
|
|
|
|
|
|
|
return the current working directory |
378
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
=item get_last_lines |
380
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
return the last n lines from a file |
382
|
|
|
|
|
|
|
|
383
|
|
|
|
|
|
|
=back |
384
|
|
|
|
|
|
|
|
385
|
|
|
|
|
|
|
=head1 AUTHOR |
386
|
|
|
|
|
|
|
|
387
|
|
|
|
|
|
|
Nigel Hamilton <nigel@trexy.com> |
388
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
=head1 SEE ALSO |
390
|
|
|
|
|
|
|
|