| 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
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | croak "Required parameter 'file' not specified in constructor" | ||||||||||||||||||||||||||||
| 24 | 10 | ▶ | unless exists $params{file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 10 | my $self = bless \%params, $class; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 10 | ▶ | $params{chomp} = 1 unless exists $params{chomp}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 10 | ▶ | $params{regex} = $params{regex} || $params{separator} || qr/\s+/; | ||||||||||||||||||||||||||
|
Condition80%4/5
$params{'regex'} || $params{'separator'} || qr/\s+/
$params{"regex"} or $params{"separator"}
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"}
| |||||||||||||||||||||||||||||
| 30 | 10 | ▶ | $params{separator} ||= ' '; | ||||||||||||||||||||||||||
|
Condition100%2/2
$params{"separator"} ||= " "
MC/DC100%1/1
$params{"separator"} ||= " "
$params{"separator"}
Truth table100%2/2
A
A= $params{"separator"}
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 10 | ▶ | if ($self->{suffix}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 61 | 6 | ▶ | if (defined $self->{current_line}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 141 | 22 | $fh->print($self->{current_line}); | |||||||||||||||||||||||||||
| 142 | 22 | ▶ | if ($self->{chomp}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 153 | 30 | my $line = $fh->getline; | |||||||||||||||||||||||||||
| 154 | 30 | ▶ | if (not defined $line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 2 | $fh->close; | |||||||||||||||||||||||||||
| 156 | 2 | delete $self->{infh}; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 30 | ▶ | if (defined $line and $self->{chomp}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $line and $self->{"chomp"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | rename $self->{tmpfile} => $self->{backup_name} | ||||||||||||||||||||||||||||
| 186 | 1 | ▶ | or croak "Can't rename $self->{tmpname} => $self->{backup_name}: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 1 | 1; | |||||||||||||||||||||||||||
| 215 | __END__ | ||||||||||||||||||||||||||||