line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
################################################################################ |
3
|
|
|
|
|
|
|
# $Id: Butler.pm 2 2010-07-21 21:56:38Z v89326 $ |
4
|
|
|
|
|
|
|
# $URL: file:///S:/svn/File-Butler/trunk/lib/File/Butler.pm $ |
5
|
|
|
|
|
|
|
################################################################################ |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# Title: File::Butler |
8
|
|
|
|
|
|
|
# Author: Kurt Kincaid |
9
|
|
|
|
|
|
|
# VERSION: 4.0.0 |
10
|
|
|
|
|
|
|
# |
11
|
|
|
|
|
|
|
################################################################################ |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package File::Butler; |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
25175
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
35
|
|
16
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
17
|
1
|
|
|
1
|
|
433
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use feature "switch"; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 NAME |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
File::Butler - Handy collection of file-related tools. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 VERSION |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Version 4.0.0 |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=cut |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
our $VERSION = '4.0.0'; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
has 'filename' => ( |
33
|
|
|
|
|
|
|
'is' => 'rw', |
34
|
|
|
|
|
|
|
'isa' => 'Str', |
35
|
|
|
|
|
|
|
'required' => 1 |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 SYNOPSIS |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
A collection of basic file manipulation tools. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
As of version 4.0.0, File::Butler is built around Moose. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
use File::Butler; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $fb = File::Butler->new( 'filename' => 'myfile.txt' ); |
47
|
|
|
|
|
|
|
my $contents = $fb->read(); |
48
|
|
|
|
|
|
|
my $retval = $fb->append( "Text to be appended." ); |
49
|
|
|
|
|
|
|
my $retval = $fb->prepend( "Text to be prepended to the beginning of the file." ); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Please note that "filename" is a required element during invocation. In |
52
|
|
|
|
|
|
|
cases where file contents are to be returned, contents are returned |
53
|
|
|
|
|
|
|
either as an array or an array reference, depending upon how the method is called. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head2 dir |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=cut |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub dir { |
62
|
|
|
|
|
|
|
my $self = shift; |
63
|
|
|
|
|
|
|
my $name = $self->{ 'filename' }; |
64
|
|
|
|
|
|
|
unless ( -d $name ) { |
65
|
|
|
|
|
|
|
die "Directory $name does not exist"; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
my @files; |
68
|
|
|
|
|
|
|
opendir( my $dh, $name ) || die "Can't opendir $name: $!"; |
69
|
|
|
|
|
|
|
@files = sort { lc( $a ) cmp lc( $b ) } readdir( $dh ); |
70
|
|
|
|
|
|
|
closedir $dh; |
71
|
|
|
|
|
|
|
chomp @files; |
72
|
|
|
|
|
|
|
my @FILES; |
73
|
|
|
|
|
|
|
foreach my $file ( @files ) { |
74
|
|
|
|
|
|
|
push( @FILES, $file ) unless $file eq "." or $file eq ".."; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
if ( wantarray() ) { |
77
|
|
|
|
|
|
|
return @FILES; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
else { |
80
|
|
|
|
|
|
|
return \@FILES; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head2 read |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=cut |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub read { |
89
|
|
|
|
|
|
|
my $self = shift; |
90
|
|
|
|
|
|
|
my @array; |
91
|
|
|
|
|
|
|
open my $FILE, "<", $self->{ 'filename' } |
92
|
|
|
|
|
|
|
or die "File $self->{ 'filename' } does not exist"; |
93
|
|
|
|
|
|
|
while ( <$FILE> ) { |
94
|
|
|
|
|
|
|
chomp; |
95
|
|
|
|
|
|
|
push( @array, $_ ); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
close $FILE; |
98
|
|
|
|
|
|
|
if ( wantarray() ) { |
99
|
|
|
|
|
|
|
return @array; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
elsif ( defined wantarray() ) { |
102
|
|
|
|
|
|
|
my $content = join "\n", @array; |
103
|
|
|
|
|
|
|
return $content; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head2 write |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub write { |
112
|
|
|
|
|
|
|
my ( $self, $content ) = @_; |
113
|
|
|
|
|
|
|
open my $OUT, ">", $self->{ 'filename' } |
114
|
|
|
|
|
|
|
or die "Unable to open $self->{ 'filename' } for writing"; |
115
|
|
|
|
|
|
|
print $OUT $content; |
116
|
|
|
|
|
|
|
close $OUT; |
117
|
|
|
|
|
|
|
return 1; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head2 append |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=cut |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub append { |
125
|
|
|
|
|
|
|
my ( $self, $content ) = @_; |
126
|
|
|
|
|
|
|
open my $OUT, ">>", $self->{ 'filename' } |
127
|
|
|
|
|
|
|
or die "Unable to open $self->{ 'filename' } for writing"; |
128
|
|
|
|
|
|
|
print $OUT $content; |
129
|
|
|
|
|
|
|
close $OUT; |
130
|
|
|
|
|
|
|
return 1; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head2 prepend |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=cut |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub prepend { |
138
|
|
|
|
|
|
|
my ( $self, $content ) = @_; |
139
|
|
|
|
|
|
|
local $/ = undef; |
140
|
|
|
|
|
|
|
open my $IN, "<", $self->{ 'filename' } |
141
|
|
|
|
|
|
|
or die "Unable to open $self->{ 'filename' } for reading"; |
142
|
|
|
|
|
|
|
my $old = <$IN>; |
143
|
|
|
|
|
|
|
close $IN; |
144
|
|
|
|
|
|
|
open my $OUT, ">", $self->{ 'filename' } |
145
|
|
|
|
|
|
|
or die "Unable to open $self->{ 'filename' } for writing"; |
146
|
|
|
|
|
|
|
print $OUT $content; |
147
|
|
|
|
|
|
|
print $OUT $old; |
148
|
|
|
|
|
|
|
close $OUT; |
149
|
|
|
|
|
|
|
return 1; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head2 srm |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub srm { |
157
|
|
|
|
|
|
|
my ( $self, $passes ) = @_; |
158
|
|
|
|
|
|
|
local $/ = undef; |
159
|
|
|
|
|
|
|
open my $IN, "<", $self->{ 'filename' } |
160
|
|
|
|
|
|
|
or die "Unable to open $self->{ 'filename' } for reading"; |
161
|
|
|
|
|
|
|
my $old = <$IN>; |
162
|
|
|
|
|
|
|
close $IN; |
163
|
|
|
|
|
|
|
my $length = length $old; |
164
|
|
|
|
|
|
|
for ( 1 .. $passes ) { |
165
|
|
|
|
|
|
|
my $text = ""; |
166
|
|
|
|
|
|
|
my $method = $_ % 10; |
167
|
|
|
|
|
|
|
my $pattern; |
168
|
|
|
|
|
|
|
given ( $method ) { |
169
|
|
|
|
|
|
|
when ( [ 1, 2, 3 ] ) { |
170
|
|
|
|
|
|
|
while ( length $text < $length ) { |
171
|
|
|
|
|
|
|
$text .= sprintf( "%.0f", rand() ); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
when ( 4 ) { |
175
|
|
|
|
|
|
|
$pattern = "010101"; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
when ( 5 ) { |
178
|
|
|
|
|
|
|
$pattern = "101010"; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
when ( 6 ) { |
181
|
|
|
|
|
|
|
$pattern = "100100"; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
when ( 7 ) { |
184
|
|
|
|
|
|
|
$pattern = "010010"; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
when ( 8 ) { |
187
|
|
|
|
|
|
|
$pattern = "001001"; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
when ( 9 ) { |
190
|
|
|
|
|
|
|
$pattern = "000000"; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
default { |
193
|
|
|
|
|
|
|
$pattern = "111111"; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
while ( length $text < $length ) { |
197
|
|
|
|
|
|
|
$text .= $pattern; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
open my $OUT, ">", $self->{ "filename" } |
200
|
|
|
|
|
|
|
or die "Unable to open $self->{ 'filename' } for writing"; |
201
|
|
|
|
|
|
|
print $OUT $text; |
202
|
|
|
|
|
|
|
close $OUT; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head2 wc |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=cut |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
sub wc { |
211
|
|
|
|
|
|
|
my $self = shift; |
212
|
|
|
|
|
|
|
my ( $lines, $words, $chars, $text ); |
213
|
|
|
|
|
|
|
local $/ = undef; |
214
|
|
|
|
|
|
|
open my $IN, "<", $self->{ 'filename' } |
215
|
|
|
|
|
|
|
or die "Unable to open $self->{ 'filename' } for reading"; |
216
|
|
|
|
|
|
|
$text = <$IN>; |
217
|
|
|
|
|
|
|
close $IN; |
218
|
|
|
|
|
|
|
$words = $text =~ s/((^|\s)\S)/$1/g; |
219
|
|
|
|
|
|
|
while ( $text =~ /\n/g ) { |
220
|
|
|
|
|
|
|
$lines++; |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
$chars = length $text; |
223
|
|
|
|
|
|
|
return $lines, $words, $chars; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=head1 AUTHOR |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
Kurt Kincaid, C<< <kurt.kincaid at gmail.com> >> |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head1 BUGS |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Please report any bugs or feature requests to C<bug-file-butler at rt.cpan.org>, or through |
233
|
|
|
|
|
|
|
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Butler>. I will be notified, and then you'll |
234
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=head1 SUPPORT |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
perldoc File::Butler |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
You can also look for information at: |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=over 4 |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Butler> |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
L<http://annocpan.org/dist/File-Butler> |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=item * CPAN Ratings |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
L<http://cpanratings.perl.org/d/File-Butler> |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=item * Search CPAN |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
L<http://search.cpan.org/dist/File-Butler/> |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=back |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
Copyright 2010 Kurt Kincaid. |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
274
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
275
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=cut |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
1; |
283
|
|
|
|
|
|
|
################################################################################ |
284
|
|
|
|
|
|
|
# EOF |