lib/File/Alter.pm

filter: Statementstmt 98.7% 76 / 77 Branchbran 72.5% 29 / 40 Conditioncond 100% 11 / 11 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 91.3% 138 / 151 CC 25 SCAR 24.7
CC
25
Cov
91%
_edit
20.3
new
3.1
insert
2.1
CRAP 11.8
↑ Summary
1 package File::Alter;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use base 'IO::String';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Params::Check qw[allow];
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use vars qw[$VERSION];
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 $VERSION = '0.01';
12  
13 ### readline() vs <> doesn't DWIM
14 ### mailed p5p at July 25, 2005 12:55:41 PM CEST
15 # As it seems, it calls the builtin readline() on <>,
16 # rather than the class' readline():
17 #
18 # BEGIN { *CORE::GLOBAL::readline = sub { 2 }; }
19 # sub X::readline { 1 };
20 # $x = bless {}, 'X';
21 # print "rl ". $x->readline . $/;
22 # print "<> ". <$x> . $/;
23 # rl 1
24 # <> 2
25  
26 2 ▶use vars qw[$LINENUMBER $LINE];
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 =head1 NAME
29  
30 File::Alter
31  
32 =head2 SYNOPSIS
33  
34 use File::Alter;
35
36 $fh = File::Alter->new( "filename.txt" );
37
38 $fh->insert( 3 => "new text\n" ); # insert text on line 3
39  
40 $fh->remove( 7 ); # remove line 7
41 $fh->remove( '$LINE =~ /foo/' ); # remove the line if
42 # it matches 'foo'
43  
44 $fh->alter( qr/2/, 'TWO' ); # replace all occurrences of
45 # 2 by TWO
46 $fh->alter( a => 'b', '$e == 4'); # replace all a by b if
47 # $e equals 4
48  
49 $str = $fh->as_string; # returns the buffer as string
50  
51 ### global variables you can use in conditions
52 $File::Alter::LINE # contents of the current line
53 $File::Alter::LINENUMBER # line number of the current line
54  
55 =head2 DESCRIPTION
56  
57 C<File::Alter> allows in memory manipulation of a file's contents.
58 The modified buffer will B<NOT> be written back to the file at any
59 point! This is useful if you want to massage read-only files, or files
60 you do not wish to alter, before they are read or used by an application.
61  
62 C<File::Alter> inherits directly from C<IO::String> adding it's own
63 methods. This means that any method that is supported by C<IO::String>
64 is supported by C<File::Alter>.
65  
66 =head1 METHODS
67  
68 =head2 $fh = File::Alter->new( FILENAME );
69  
70 Creates a new C<File::Alter> filehandle object. The arguments get passed
71 straight to C<IO::String::new>, so even more complicated strings are
72 accepted. Please note though that opening a file for writing makes no
73 sense, as you're only able to modify the files contents in memory, without
74 writing it to disk.
75  
76 =cut
77  
78  
79 5 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
80 5 my $class = shift;
81 5 ▶ my @args = @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@args) = or return 5 0 5
82  
83 5 ▶ my $fh = IO::File->new( @args ) or (
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new(@args) or carp("Could not create new filehandle from args '@{args}': $!"), return 5 0 5
84 carp( "Could not create new filehandle from args '@args': $!" ),
85 return
86 );
87  
88 5 my $self = __PACKAGE__->SUPER::new( do { local $/; <$fh> } );
89
90 5 return $self;
91 }
92  
93 =head2 $string = $fh->as_string;
94  
95 Returns the stringified version of the internal buffer
96  
97 =cut
98  
99 8 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
100 8 my $self = shift;
101 8 my $pos = $self->pos;
102
103 8 $self->setpos(0);
104 8 my $str = do { local $/; <$self> };
105
106 8 $self->setpos( $pos );
107
108 8 return $str;
109 }
110  
111 =head2 $bool = $fh->insert( $line => $text );
112  
113 Inserts the given text at linenumber C<$line>. This text can be multiline
114 if desired, as it's a plain insert. That means that if you want this
115 text to be on it's own line, you should add a newline to it.
116  
117 =cut
118  
119 3 ▶sub insert {
Subroutine insert: called CC=2 SCAR=7.2
Pod documented
120 3 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 3 0 3
121  
122 3 $self->_edit( insert => @_ );
123 }
124  
125 =head2 $bool = $fh->alter( $find => $replace, [$condition] );
126  
127 Looks on a per-line basis for the string specified by C<$find> and tries
128 to replace that with C<$replace>. Note that C<$find> can be a C<qr//>
129 object if you so desire.
130  
131 If you specify a condition, the substitute will only be attempted if the
132 condition evaluates to C<true>. You can use some of C<File::Alter>'s
133 global variables to make conditions based on line numbers and contents;
134 see the C<GLOBAL VARIABLES> section for details.
135  
136 =cut
137  
138 3 ▶sub alter {
Subroutine alter: called CC=2 SCAR=7.2
Pod documented
139 3 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 3 0 3
140  
141 3 $self->_edit( alter => @_ );
142 }
143  
144 =head2 $bool = $fh->remove( $line | $condition );
145  
146 Removes a line based on either line number or condition.
147  
148 If you specify a condition, the remove will only be done if the
149 condition evaluates to C<true>. You can use some of C<File::Alter>'s
150 global variables to make conditions based on line numbers and contents;
151 see the C<GLOBAL VARIABLES> section for details.
152  
153 =cut
154  
155 3 ▶sub remove {
Subroutine remove: called CC=2 SCAR=7.2
Pod documented
156 3 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 3 0 3
157
158 3 $self->_edit( remove => @_ );
159 }
160  
161 9 ▶sub _edit {
Subroutine _edit: called CC=20 SCAR=30.1
162 9 my $self = shift;
163 9 my $type = shift;
164
165 9 ▶ unless( allow( $type, [qw|alter insert remove|] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (allow($type, ["alter", "insert", "remove"])) 0 9 9
166 0 carp( "Unknown type '$type' -- can not comply" ),
167 return
168 };
169
170
171 ### first, reset the position to 0
172 9 $self->setpos(0);
173  
174 ### $. is actually not the line number, but the amount of times
175 ### you've read a line from a filehandle
176 9 local $LINENUMBER;
177
178 9 my ($buf);
179  
180 9 ▶ if( $type eq 'alter' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'alter') { } 3 6 9
elsif ($type eq 'insert') { } 3 3 6
elsif ($type eq 'remove') { } 3 0 3
181 3 ▶ my $find = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $find = shift() or return 3 0 3
182 3 ▶ my $replace = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $replace = shift() or return 3 0 3
183 3 ▶ my $cond = shift || 1;
Condition100%2/2
shift() || 1
l!l
12
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
184
185 3 ▶ while( $LINE = <$self> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($File::Alter::LINE = readline $self)) 18 3 21
186 18 ▶ eval { $LINE =~ s/$find/$replace/ } if eval $cond;
Branch: 1 branch
BranchTrueFalseTotal
if eval $cond 13 5 18
187 18 $buf .= $LINE;
188 }
189
190 } elsif ( $type eq 'insert' ) {
191 3 ▶ my $line = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $line = shift() or return 3 0 3
192 3 ▶ my $text = shift; return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 3 3
193  
194  
195 3 ▶ while( $LINE = <$self> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($File::Alter::LINE = readline $self)) 18 3 21
196 18 ▶ $buf .= $text if ++$LINENUMBER eq $line;
Branch: 1 branch
BranchTrueFalseTotal
if ++$File::Alter::LINENUMBER eq $line 3 15 18
197 18 $buf .= $LINE;
198 }
199 } elsif ( $type eq 'remove' ) {
200 3 my $line; my $cond;
201
202 3 ▶ $_[0] !~ /\D/ ? $line = $_[0] : $cond = $_[0];
Branch: 1 branch
BranchTrueFalseTotal
!($_[0] =~ /\D/) ? : 2 1 3
203  
204 3 ▶ while( $LINE = <$self> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($File::Alter::LINE = readline $self)) 17 3 20
205 17 ++$LINENUMBER;
206
207 17 ▶ if( ($line and $line eq $LINENUMBER) or
Branch: 1 branch
BranchTrueFalseTotal
if ($line and $line eq $File::Alter::LINENUMBER or $cond and eval $cond) 3 14 17
Condition100%9/9
$line and $line eq $File::Alter::LINENUMBER or $cond and eval $cond
l!l&&r!l&&!r
2114
$line and $line eq $File::Alter::LINENUMBER
!ll&&!rl&&r
692
$cond and eval $cond
!ll&&!rl&&r
951
MC/DC75%3/4
$line and $line eq $File::Alter::LINENUMBER or $cond and eval $cond
$line $line eq $File::Alter::LINENUMBER $cond eval $cond
Truth table57%4/7
A and B or C and D
A= $lineB= $line eq $File::Alter::LINENUMBERC= $condD= eval $cond
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
208 ($cond and eval $cond )
209 ) {
210 3 next;
211 }
212
213 14 $buf .= $LINE;
214 }
215 }
216
217 ### we changed stuff from the FH... we need to truncate it to 0
218 ### and reprint the buffer to make sure there's no trailing garbage
219 9 $self->truncate(0);
220  
221 ### set to 0, so to print at the beginning
222 9 $self->setpos(0);
223 9 $self->print( $buf );
224
225 9 $self->setpos(0);
226  
227 9 return 1;
228 }
229  
230 =head1 GLOBAL VARIABLES
231  
232 =head2 $File::Alter::LINE
233  
234 Contains the contents of the current line being read. You can use this
235 in a condition if you wish to only have it apply relative to a certain
236 line number. For example:
237  
238 $fh->remove( '$LINE =~ /foo/ or $LINE =~ /bar/' );
239
240 To remove all lines that contain C<foo> or C<bar>.
241  
242 =head2 $File::Alter::LINENUMBER
243  
244 Containts the current line number of the file being read. You can use
245 this in a condition if you wish to only have it apply relative to a certain
246 line number. For example:
247  
248 $fh->remove( '$LINENUMBER > 20 and $LINENUMBER < 30' );
249
250 To remove all lines between 20 and 30.
251  
252 =head1 CAVEATS
253  
254 =head2 Filehandle position always reset to C<0> after modification
255  
256 As we're modifying the filehandle on every C<alter>, C<insert> and
257 C<replace>, we can not be certain that the position the last C<read>
258 was from is still correct (especially since the position is in bytes),
259 nor can we be sure it's desirable.
260  
261 So, after every alteration of the in memory string using above mentioned
262 methods, the file's position is set to C<0>, so any read will start again
263 at the beginning of the file
264  
265 =head2 use $File::Alter::LINENUMBER rather than $.
266  
267 C<$.> isn't actually C<the current line number of the last active
268 filehandle> but the amount of times a line has been read from the last
269 active filehandle.
270  
271 This is a subtle but important difference, seeing when you loop over a
272 file as a whole, and then read the first line again, C<$.> would hold
273 C<lines in the file + 1> rather than C<1>.
274  
275 C<$File::Alter::LINENUMBER> does what you expect here and would hold C<1>.
276  
277 =head1 AUTHOR
278  
279 This module by
280 Jos Boumans E<lt>kane@cpan.orgE<gt>.
281  
282 =head1 COPYRIGHT
283  
284 This module is
285 copyright (c) 2005 Jos Boumans E<lt>kane@cpan.orgE<gt>.
286 All rights reserved.
287  
288 This library is free software;
289 you may redistribute and/or modify it under the same
290 terms as Perl itself.
291  
292 =cut
293  
294  
295 2 1;
↑ Summary