blib/lib/File/Inplace.pm

filter: Statementstmt 100% 117 / 117 Branchbran 77.2% 34 / 44 Conditioncond 90.0% 9 / 10 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 23 / 23 Podpod 90.0% 9 / 10 totaltotal 93.7% 196 / 209 CC 30 SCAR 13.9
CC
30
Cov
94%
new
10.2
_read_next_line
5.0
commit_to_backup
3.1
CRAP 4.0
↑ Summary
1 package File::Inplace;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use Carp qw/carp croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Basename qw/dirname/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Temp qw/tempfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.20';
12  
13 1 my @allowed_options = qw/chomp regex separator suffix file/;
14 1 my %allowed_options = map { $_ => 1 } @allowed_options;
15  
16 10 ▶sub new {
Subroutine new: called CC=10 SCAR=23.2
Pod documented
17 10 my $class = shift;
18 10 my %params = @_;
19  
20 10 for my $opt (keys %params) {
21 15 ▶ croak "Invalid constructor option '$opt'" unless exists $allowed_options{$opt};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $allowed_options{$opt} 0 15 15
22 }
23 croak "Required parameter 'file' not specified in constructor"
24 10 ▶ unless exists $params{file};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"file"} 0 10 10
25  
26 10 my $self = bless \%params, $class;
27  
28 10 ▶ $params{chomp} = 1 unless exists $params{chomp};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"chomp"} 9 1 10
29 10 ▶ $params{regex} = $params{regex} || $params{separator} || qr/\s+/;
Condition80%4/5
$params{'regex'} || $params{'separator'} || qr/\s+/
l!l
19
$params{"regex"} or $params{"separator"}
l!l&&r!l&&!r
019
MC/DC50%1/2
$params{'regex'} || $params{'separator'} || qr/\s+/
$params{"regex"} $params{"separator"}
Truth table66%2/3
A or B
A= $params{"regex"}B= $params{"separator"}
ABresult
1X1
011
000
30 10 ▶ $params{separator} ||= ' ';
Condition100%2/2
$params{"separator"} ||= " "
l!l
19
MC/DC100%1/1
$params{"separator"} ||= " "
$params{"separator"}
Truth table100%2/2
A
A= $params{"separator"}
Aresult
11
00
31  
32 10 ▶ if ($self->{suffix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"suffix"}) 3 7 10
33 3 $self->{backup_name} = $self->{file} . $self->{suffix};
34 copy($self->{file} => $self->{backup_name})
35 3 ▶ or croak "error creating backup: $!";
Branch: 1 branch
BranchTrueFalseTotal
copy($self->{"file"}, $self->{"backup_name"}) or croak("error creating backup: $!") 3 0 3
36 }
37  
38 10 $self->_open_input_file;
39 10 $self->_open_output_file;
40  
41 10 $self->{current_line} = undef;
42  
43 10 return $self;
44 }
45  
46 27 ▶sub has_lines {
Subroutine has_lines: called CC=2 SCAR=6.9
Pod documented
47 27 my $self = shift;
48  
49 27 ▶ return 1 if not $self->{infh}->eof();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"infh"}->eof 20 7 27
50 7 return 0;
51 }
52  
53 26 ▶sub next_line {
Subroutine next_line: called CC=3 SCAR=11.0
Pod documented
54 26 my $self = shift;
55  
56 26 $self->_write_current_line;
57  
58 26 $self->{current_line} = $self->_read_next_line();
59  
60 26 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 6 20 26
61 6 ▶ if (defined $self->{current_line}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'current_line'}) { } 5 1 6
62 5 return ($self->{current_line});
63 }
64 else {
65 1 return ();
66 }
67 }
68  
69 20 return $self->{current_line};
70 }
71  
72 2 ▶sub next_line_split {
Subroutine next_line_split: called CC=1 SCAR=0.0
Pod documented
73 2 my $self = shift;
74  
75 2 my $line = $self->next_line;
76  
77 2 return split $self->{regex}, $line;
78 }
79  
80 1 ▶sub all_lines {
Subroutine all_lines: called CC=3 SCAR=11.0
Pod documented
81 1 my $self = shift;
82  
83 croak "cannot use all_lines after any lines have been read"
84 1 ▶ if defined $self->{current_line};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"current_line"} 0 1 1
85  
86 1 my @ret;
87 1 while (1) {
88 4 my $line = $self->_read_next_line;
89 4 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 3 4
90 3 push @ret, $line;
91 }
92  
93 1 return @ret;
94 }
95  
96 9 ▶sub replace_line {
Subroutine replace_line: called CC=2 SCAR=6.9
Pod documented
97 9 my $self = shift;
98  
99 9 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 7 2 9
100 7 $self->{current_line} = shift;
101 }
102 else {
103 2 $self->{current_line} = join($self->{separator}, @_);
104 }
105 }
106  
107 1 ▶sub replace_lines {
Subroutine replace_lines: called CC=3 SCAR=11.0
Pod undocumented
108 1 my $self = shift;
109 1 my @lines = @_;
110  
111 1 my $fh = $self->{outfh};
112 1 for my $line (@lines) {
113 3 $fh->print($line);
114 3 ▶ if ($self->{chomp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"chomp"}) 3 0 3
115 3 $fh->print($/);
116 }
117 }
118 }
119  
120 10 ▶sub _open_input_file {
Subroutine _open_input_file: called CC=2 SCAR=7.1
121 10 my $self = shift;
122  
123 10 $self->{infh} = new IO::File("<$self->{file}");
124 10 ▶ croak "open $self->{file}: $!" if not $self->{infh};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"infh"} 0 10 10
125 }
126  
127 10 ▶sub _open_output_file {
Subroutine _open_output_file: called CC=1 SCAR=0.0
128 10 my $self = shift;
129  
130 10 my $dir = dirname $self->{file};
131 10 my ($tmpfh, $tmpname) = tempfile(DIR => $dir);
132 10 $self->{outfh} = bless $tmpfh, "IO::Handle";
133 10 $self->{tmpfile} = $tmpname;
134 }
135  
136 33 ▶sub _write_current_line {
Subroutine _write_current_line: called CC=3 SCAR=11.0
137 33 my $self = shift;
138  
139 33 my $fh = $self->{outfh};
140 33 ▶ if (defined $self->{current_line}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"current_line"}) 22 11 33
141 22 $fh->print($self->{current_line});
142 22 ▶ if ($self->{chomp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"chomp"}) 19 3 22
143 19 $fh->print($/);
144 }
145 }
146 }
147  
148 30 ▶sub _read_next_line {
Subroutine _read_next_line: called CC=5 SCAR=16.1
149 30 my $self = shift;
150  
151 30 my $fh = $self->{infh};
152 30 ▶ return undef unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 30 30
153 30 my $line = $fh->getline;
154 30 ▶ if (not defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 2 28 30
155 2 $fh->close;
156 2 delete $self->{infh};
157 }
158  
159 30 ▶ if (defined $line and $self->{chomp}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line and $self->{"chomp"}) 25 5 30
Condition100%3/3
defined $line and $self->{"chomp"}
!ll&&!rl&&r
2325
MC/DC100%2/2
defined $line and $self->{"chomp"}
defined $line $self->{"chomp"}
Truth table100%3/3
A and B
A= defined $lineB= $self->{"chomp"}
ABresult
0X0
100
111
160 25 chomp $line;
161 }
162  
163 30 return $line;
164 }
165  
166 6 ▶sub commit {
Subroutine commit: called CC=2 SCAR=7.0
Pod documented
167 6 my $self = shift;
168  
169 6 $self->_write_current_line;
170  
171 rename $self->{tmpfile} => $self->{file}
172 6 ▶ or croak "Can't rename $self->{tmpname} => $self->{file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $self->{"tmpfile"}, $self->{"file"} or croak(qq[Can't rename $self->{"tmpname"} => $self->{"file"}: $!]) 6 0 6
173  
174 6 $self->_close_all();
175 }
176  
177 1 ▶sub commit_to_backup {
Subroutine commit_to_backup: called CC=3 SCAR=11.3
Pod documented
178 1 my $self = shift;
179  
180 1 $self->_write_current_line;
181  
182 croak "cannot commit_to_backup if no backup file is in use"
183 1 ▶ unless $self->{backup_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"backup_name"} 0 1 1
184  
185 rename $self->{tmpfile} => $self->{backup_name}
186 1 ▶ or croak "Can't rename $self->{tmpname} => $self->{backup_name}: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $self->{"tmpfile"}, $self->{"backup_name"} or croak(qq[Can't rename $self->{"tmpname"} => $self->{"backup_name"}: $!]) 1 0 1
187  
188 1 $self->_close_all();
189 }
190  
191 2 ▶sub rollback {
Subroutine rollback: called CC=1 SCAR=0.0
Pod documented
192 2 my $self = shift;
193  
194 2 $self->_close_all();
195 2 unlink $self->{tmpfile};
196 }
197  
198 10 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
199 10 my $self = shift;
200  
201 10 $self->_close_all();
202 10 unlink $self->{tmpfile};
203 }
204  
205 19 ▶sub _close_all {
Subroutine _close_all: called CC=3 SCAR=11.0
206 19 my $self = shift;
207  
208 19 for my $handle (qw/infh outfh/) {
209 $self->{$handle}->close()
210 38 ▶ if $self->{$handle};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$handle} 34 4 38
211 }
212 }
213  
214 1 1;
215 __END__
↑ Summary