blib/lib/File/Butler.pm

filter: Statementstmt 14.1% 15 / 106 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 36.3% 4 / 11 Podpod 100% 7 / 7 totaltotal 15.7% 26 / 165 CC 22 SCAR 33.3
CC
22
Cov
10%
dir
56.0
srm
42.0
read
30.0
CRAP 27.9
↑ Summary
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 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use feature "switch";
Subroutine BEGIN: called CC=1 SCAR=0.0
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 2 our $VERSION = '4.0.0';
31
32 2 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 0 ▶sub dir {
Subroutine dir: not called CC=7 SCAR=40.3
Pod documented
62 0 my $self = shift;
63 0 my $name = $self->{ 'filename' };
64 0 ▶ unless ( -d $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $name) 0 0 0
65 0 die "Directory $name does not exist";
66 }
67 0 my @files;
68 0 ▶ opendir( my $dh, $name ) || die "Can't opendir $name: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $name or die "Can't opendir ${name}: $!" 0 0 0
69 0 @files = sort { lc( $a ) cmp lc( $b ) } readdir( $dh );
70 0 closedir $dh;
71 0 chomp @files;
72 0 my @FILES;
73 0 foreach my $file ( @files ) {
74 0 ▶ push( @FILES, $file ) unless $file eq "." or $file eq "..";
Branch: 1 branch
BranchTrueFalseTotal
unless $file eq "." or $file eq ".." 0 0 0
Condition0%0/3
$file eq "." or $file eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$file eq "." or $file eq ".."
$file eq "." $file eq ".."
Truth table0%0/3
A or B
A= $file eq "."B= $file eq ".."
ABresult
1X1
011
000
75 }
76 0 ▶ if ( wantarray() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
77 0 return @FILES;
78 }
79 else {
80 0 return \@FILES;
81 }
82 }
83
84 =head2 read
85
86 =cut
87
88 0 ▶sub read {
Subroutine read: not called CC=5 SCAR=34.0
Pod documented
89 0 my $self = shift;
90 0 my @array;
91 0 ▶ open my $FILE, "<", $self->{ 'filename' }
Branch: 1 branch
BranchTrueFalseTotal
open my $FILE, "<", $self->{"filename"} or die qq[File $self->{"filename"} does not exist] 0 0 0
92 or die "File $self->{ 'filename' } does not exist";
93 0 ▶ while ( <$FILE> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $FILE)) 0 0 0
94 0 chomp;
95 0 push( @array, $_ );
96 }
97 0 close $FILE;
98 0 ▶ if ( wantarray() ) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
elsif (defined wantarray) { } 0 0 0
99 0 return @array;
100 }
101 elsif ( defined wantarray() ) {
102 0 my $content = join "\n", @array;
103 0 return $content;
104 }
105 }
106
107 =head2 write
108
109 =cut
110
111 0 ▶sub write {
Subroutine write: not called CC=2 SCAR=17.9
Pod documented
112 0 my ( $self, $content ) = @_;
113 0 ▶ open my $OUT, ">", $self->{ 'filename' }
Branch: 1 branch
BranchTrueFalseTotal
open my $OUT, ">", $self->{"filename"} or die qq[Unable to open $self->{"filename"} for writing] 0 0 0
114 or die "Unable to open $self->{ 'filename' } for writing";
115 0 print $OUT $content;
116 0 close $OUT;
117 0 return 1;
118 }
119
120 =head2 append
121
122 =cut
123
124 0 ▶sub append {
Subroutine append: not called CC=2 SCAR=17.9
Pod documented
125 0 my ( $self, $content ) = @_;
126 0 ▶ open my $OUT, ">>", $self->{ 'filename' }
Branch: 1 branch
BranchTrueFalseTotal
open my $OUT, ">>", $self->{"filename"} or die qq[Unable to open $self->{"filename"} for writing] 0 0 0
127 or die "Unable to open $self->{ 'filename' } for writing";
128 0 print $OUT $content;
129 0 close $OUT;
130 0 return 1;
131 }
132
133 =head2 prepend
134
135 =cut
136
137 0 ▶sub prepend {
Subroutine prepend: not called CC=3 SCAR=24.8
Pod documented
138 0 my ( $self, $content ) = @_;
139 0 local $/ = undef;
140 0 ▶ open my $IN, "<", $self->{ 'filename' }
Branch: 1 branch
BranchTrueFalseTotal
open my $IN, "<", $self->{"filename"} or die qq[Unable to open $self->{"filename"} for reading] 0 0 0
141 or die "Unable to open $self->{ 'filename' } for reading";
142 0 my $old = <$IN>;
143 0 close $IN;
144 0 ▶ open my $OUT, ">", $self->{ 'filename' }
Branch: 1 branch
BranchTrueFalseTotal
open my $OUT, ">", $self->{"filename"} or die qq[Unable to open $self->{"filename"} for writing] 0 0 0
145 or die "Unable to open $self->{ 'filename' } for writing";
146 0 print $OUT $content;
147 0 print $OUT $old;
148 0 close $OUT;
149 0 return 1;
150 }
151
152 =head2 srm
153
154 =cut
155
156 0 ▶sub srm {
Subroutine srm: not called CC=6 SCAR=37.4
Pod documented
157 0 my ( $self, $passes ) = @_;
158 0 local $/ = undef;
159 0 ▶ open my $IN, "<", $self->{ 'filename' }
Branch: 1 branch
BranchTrueFalseTotal
open my $IN, "<", $self->{"filename"} or die qq[Unable to open $self->{"filename"} for reading] 0 0 0
160 or die "Unable to open $self->{ 'filename' } for reading";
161 0 my $old = <$IN>;
162 0 close $IN;
163 0 my $length = length $old;
164 0 for ( 1 .. $passes ) {
165 0 my $text = "";
166 0 my $method = $_ % 10;
167 0 my $pattern;
168 0 given ( $method ) {
169 0 when ( [ 1, 2, 3 ] ) {
170 0 ▶ while ( length $text < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text < $length) 0 0 0
171 0 $text .= sprintf( "%.0f", rand() );
172 }
173 }
174 0 when ( 4 ) {
175 0 $pattern = "010101";
176 }
177 0 when ( 5 ) {
178 0 $pattern = "101010";
179 }
180 0 when ( 6 ) {
181 0 $pattern = "100100";
182 }
183 0 when ( 7 ) {
184 0 $pattern = "010010";
185 }
186 0 when ( 8 ) {
187 0 $pattern = "001001";
188 }
189 0 when ( 9 ) {
190 0 $pattern = "000000";
191 }
192 0 default {
193 0 $pattern = "111111";
194 }
195 }
196 0 ▶ while ( length $text < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text < $length) 0 0 0
197 0 $text .= $pattern;
198 }
199 0 ▶ open my $OUT, ">", $self->{ "filename" }
Branch: 1 branch
BranchTrueFalseTotal
open my $OUT, ">", $self->{"filename"} or die qq[Unable to open $self->{"filename"} for writing] 0 0 0
200 or die "Unable to open $self->{ 'filename' } for writing";
201 0 print $OUT $text;
202 0 close $OUT;
203 }
204 }
205
206 =head2 wc
207
208 =cut
209
210 0 ▶sub wc {
Subroutine wc: not called CC=3 SCAR=24.8
Pod documented
211 0 my $self = shift;
212 0 my ( $lines, $words, $chars, $text );
213 0 local $/ = undef;
214 0 ▶ open my $IN, "<", $self->{ 'filename' }
Branch: 1 branch
BranchTrueFalseTotal
open my $IN, "<", $self->{"filename"} or die qq[Unable to open $self->{"filename"} for reading] 0 0 0
215 or die "Unable to open $self->{ 'filename' } for reading";
216 0 $text = <$IN>;
217 0 close $IN;
218 0 $words = $text =~ s/((^|\s)\S)/$1/g;
219 0 ▶ while ( $text =~ /\n/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\n/g) 0 0 0
220 0 $lines++;
221 }
222 0 $chars = length $text;
223 0 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 2 1;
283 ################################################################################
284 # EOF
↑ Summary