| 1 | package File::Edit::Portable; | ||||||||||||||||||||||||||||
| 2 | 17 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 0 | ▶ | $SIG{__WARN__} = sub { confess(shift); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 7 | 17 | our $VERSION = '1.25'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 17 | ▶ | use Carp qw(confess croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 17 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 17 | ▶ | use Fcntl qw(:flock); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 17 | ▶ | use File::Find::Rule; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 17 | ▶ | use File::Temp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 17 | ▶ | use POSIX qw(uname); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 17 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 17 | 17 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 18 | recsep | ||||||||||||||||||||||||||||
| 19 | platform_recsep | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub new { | ||||||||||||||||||||||||||||
| 23 | 60 | ▶ | return bless {}, shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | sub read { | ||||||||||||||||||||||||||||
| 26 | 112 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 112 | my ($file, $testing); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 112 | ▶ | if ($_[0] eq 'file'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 8 | $self->_config(@_); | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | else { | ||||||||||||||||||||||||||||
| 33 | 104 | $file = shift; | |||||||||||||||||||||||||||
| 34 | 104 | ▶ | $testing = shift if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 104 | $self->_config(file => $file, testing => $testing); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 112 | $file = $self->{file}; | |||||||||||||||||||||||||||
| 39 | 112 | $testing = $self->{testing}; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 112 | ▶ | if (! $file){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 1 | confess "read() requires a file name sent in!"; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 111 | $self->recsep($file); | |||||||||||||||||||||||||||
| 46 | 111 | $self->{files}{$file}{is_read} = 1; | |||||||||||||||||||||||||||
| 47 | 111 | $self->{files}{$file}{recsep} = $self->{recsep}; | |||||||||||||||||||||||||||
| 48 | 111 | $self->{reads}{count} = keys %{ $self->{files} }; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 111 | my $fh; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 111 | ▶ | if (! wantarray){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 54 | $fh = $self->_handle($file); | |||||||||||||||||||||||||||
| 54 | 54 | return $fh; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | else { | ||||||||||||||||||||||||||||
| 57 | 57 | $fh = $self->_binmode_handle($file); | |||||||||||||||||||||||||||
| 58 | 57 | my @contents = <$fh>; | |||||||||||||||||||||||||||
| 59 | 57 | ▶ | close $fh or confess "read() can't close file $file!: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 57 | ▶ | if (! $testing){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 55 | for (@contents){ | |||||||||||||||||||||||||||
| 63 | 474 | $_ = $self->_strip_ends($_); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 57 | return @contents; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | sub write { | ||||||||||||||||||||||||||||
| 70 | 76 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=17 SCAR=28.3
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 76 | my %p = @_; | |||||||||||||||||||||||||||
| 72 | 76 | $self->_config(%p); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 76 | ▶ | if (! $self->{file}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 2 | confess "write() requires a file to be passed in!"; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 74 | ▶ | my $reads_count = $self->{reads}{count} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'reads'}{'count'} || 0
MC/DC100%1/1
$self->{'reads'}{'count'} || 0
$self->{'reads'}{'count'}
Truth table100%2/2
A
A= $self->{'reads'}{'count'}
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 74 | ▶ | if ($reads_count > 1 && ! $p{file}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$reads_count > 1 and not $p{"file"}
MC/DC100%2/2
$reads_count > 1 and not $p{"file"}
$reads_count > 1
not $p{"file"}
Truth table100%3/3
A and not(B)
A= $reads_count > 1B= not $p{"file"}
| |||||||||||||||||||||||||||||
| 81 | confess "\nif calling write() with more than one read() open, you must " . | ||||||||||||||||||||||||||||
| 82 | "send in a file name with the 'file' parameter so we know " . | ||||||||||||||||||||||||||||
| 83 | "which file to write. You currently have the following files " . | ||||||||||||||||||||||||||||
| 84 | 2 | "open: " . join(' ', keys %{ $self->{files} }) . "\n"; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 72 | ▶ | if (! $self->{contents}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 1 | confess "write() requires 'contents' param sent in"; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 71 | my $file = $self->{file}; # needed for cleanup of open file list | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 71 | ▶ | if (! $self->{files}{$file}{is_read}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 6 | $self->{files}{$file}{recsep} = $self->recsep($file); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 71 | ▶ | $self->{file} = $self->{copy} if $self->{copy}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 71 | my $wfh = $self->_binmode_handle($self->{file}, 'w'); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # certain FreeBSD versions on amd64 don't work | ||||||||||||||||||||||||||||
| 101 | # with flock() | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 71 | my @os = uname(); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 71 | ▶ | unless ($os[0] eq 'FreeBSD' && $os[-1] eq 'amd64'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$os[0] eq "FreeBSD" and $os[-1] eq "amd64"
MC/DC0%0/2
$os[0] eq "FreeBSD" and $os[-1] eq "amd64"
$os[0] eq "FreeBSD"
$os[-1] eq "amd64"
Truth table33%1/3
A and B
A= $os[0] eq "FreeBSD"B= $os[-1] eq "amd64"
| |||||||||||||||||||||||||||||
| 106 | 71 | flock $wfh, LOCK_EX; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | my $recsep = defined $self->{custom_recsep} | ||||||||||||||||||||||||||||
| 110 | ? $self->{custom_recsep} | ||||||||||||||||||||||||||||
| 111 | 71 | ▶ | : $self->{files}{$file}{recsep}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 71 | my $contents = $self->{contents}; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 71 | ▶ | if (ref($contents) eq 'GLOB' || ref($contents) eq 'File::Temp') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $contents eq "GLOB" or ref $contents eq "File::Temp"
MC/DC100%2/2
ref $contents eq "GLOB" or ref $contents eq "File::Temp"
ref $contents eq "GLOB"
ref $contents eq "File::Temp"
Truth table100%3/3
A or B
A= ref $contents eq "GLOB"B= ref $contents eq "File::Temp"
| |||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||
| 117 | 49 | my $warn; | |||||||||||||||||||||||||||
| 118 | 49 | ▶ | local $SIG{__WARN__} = sub { $warn = shift; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 49 | seek $contents, 0, 0; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 49 | ▶ | if ($warn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 1 | confess "\nthe file handle you're passing into write() as ". | |||||||||||||||||||||||||||
| 124 | "the contents param has already been closed\n"; | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | }; | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 48 | ▶ | while (<$contents>){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 221 | $_ = $self->_strip_ends($_); | |||||||||||||||||||||||||||
| 130 | 221 | print $wfh $_ . $recsep; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 48 | close $contents; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||
| 135 | 22 | for (@$contents){ | |||||||||||||||||||||||||||
| 136 | 250 | $_ = $self->_strip_ends($_); | |||||||||||||||||||||||||||
| 137 | 250 | print $wfh $_ . $recsep; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 70 | close $wfh; | |||||||||||||||||||||||||||
| 142 | 70 | delete $self->{files}{$file}; # cleanup open list | |||||||||||||||||||||||||||
| 143 | 70 | ▶ | $self->{reads}{count} = 0 if keys %{ $self->{files} } == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 70 | return 1; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | sub splice { | ||||||||||||||||||||||||||||
| 148 | 17 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
splice: called CC=16 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 17 | $self->_config(@_); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 17 | my $file = $self->{file}; | |||||||||||||||||||||||||||
| 152 | 17 | my $copy = $self->{copy}; | |||||||||||||||||||||||||||
| 153 | 17 | my $insert = $self->{insert}; | |||||||||||||||||||||||||||
| 154 | 17 | ▶ | my $limit = defined $self->{limit} ? $self->{limit} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 17 | ▶ | if (! $insert){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 1 | confess "splice() requires insert => [aref] param"; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 16 | my ($line, $find) = ($self->{line}, $self->{find}); | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 16 | ▶ | if (! defined $line && ! defined $find){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $line or defined $find
MC/DC100%2/2
defined $line or defined $find
defined $line
defined $find
Truth table100%3/3
A or B
A= defined $lineB= defined $find
| |||||||||||||||||||||||||||||
| 163 | 1 | confess | |||||||||||||||||||||||||||
| 164 | "splice() requires either the 'line' or 'find' parameter sent in."; | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 15 | ▶ | if (defined $line && defined $find){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $line and defined $find
MC/DC100%2/2
defined $line and defined $find
defined $line
defined $find
Truth table100%3/3
A and B
A= defined $lineB= defined $find
| |||||||||||||||||||||||||||||
| 168 | 1 | warn | |||||||||||||||||||||||||||
| 169 | "splice() can't search for both line and find. Operating on 'line'."; | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 15 | my @contents = $self->read($file); | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 14 | ▶ | if (defined $line){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 4 | ▶ | if ($line !~ /^[0-9]+$/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 1 | confess "splice() requires its 'line' param to contain only an " . | |||||||||||||||||||||||||||
| 177 | "integer. You supplied: $line\n"; | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | 3 | splice @contents, $line, 0, @$insert; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 13 | ▶ | if (defined $find && ! defined $line){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $find and not defined $line
MC/DC100%2/2
defined $find and not defined $line
defined $find
not defined $line
Truth table100%3/3
A and not(B)
A= defined $findB= not defined $line
| |||||||||||||||||||||||||||||
| 183 | 10 | ▶ | $find = qr{$find} if ! ref $find ne 'Regexp'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 10 | my $i = 0; | |||||||||||||||||||||||||||
| 186 | 10 | my $inserts = 0; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 10 | for (@contents){ | |||||||||||||||||||||||||||
| 189 | 75 | $i++; | |||||||||||||||||||||||||||
| 190 | 75 | ▶ | if (/$find/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 21 | $inserts++; | |||||||||||||||||||||||||||
| 192 | 21 | splice @contents, $i, 0, @$insert; | |||||||||||||||||||||||||||
| 193 | 21 | ▶ | if ($limit){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 17 | ▶ | last if $inserts == $limit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 13 | $self->write(contents => \@contents, copy => $copy); | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 13 | return @contents; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | sub dir { | ||||||||||||||||||||||||||||
| 205 | 18 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
dir: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 206 | 18 | $self->_config(@_); | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 18 | my $recsep = $self->{custom_recsep}; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 18 | my @types; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 18 | ▶ | if ($self->{types}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 8 | @types = @{ $self->{types} }; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | else { | ||||||||||||||||||||||||||||
| 216 | 10 | @types = qw(*); | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 18 | my $find = File::Find::Rule->new; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 18 | ▶ | $find->maxdepth($self->{maxdepth}) if $self->{maxdepth}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 18 | $find->file; | |||||||||||||||||||||||||||
| 223 | 18 | $find->name(@types); | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 18 | my @files = $find->in($self->{dir}); | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 18 | ▶ | return @files if $self->{list}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 10 | for my $file (@files){ | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 23 | my $fh = $self->read($file); | |||||||||||||||||||||||||||
| 232 | 23 | my $wfh = $self->tempfile; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 23 | ▶ | while(<$fh>){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 69 | print $wfh $_; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | 23 | close $fh; | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 23 | ▶ | $self->write( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | file => $file, | ||||||||||||||||||||||||||||
| 241 | contents => $wfh, | ||||||||||||||||||||||||||||
| 242 | recsep => defined $recsep | ||||||||||||||||||||||||||||
| 243 | ? $recsep | ||||||||||||||||||||||||||||
| 244 | : $self->platform_recsep, | ||||||||||||||||||||||||||||
| 245 | ); | ||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 10 | return @files; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | sub recsep { | ||||||||||||||||||||||||||||
| 251 | 226 | ▶ | my $self = ref $_[0] eq __PACKAGE__ | ||||||||||||||||||||||||||
|
Subroutine
recsep: called CC=8 SCAR=20.8
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | ? shift | ||||||||||||||||||||||||||||
| 253 | : __PACKAGE__->new; | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 226 | my $file = shift; | |||||||||||||||||||||||||||
| 256 | 226 | my $want = shift; | |||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 226 | my $fh; | |||||||||||||||||||||||||||
| 259 | 226 | eval { | |||||||||||||||||||||||||||
| 260 | 226 | $fh = $self->_binmode_handle($file); | |||||||||||||||||||||||||||
| 261 | }; | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 226 | ▶ | if ($@ || -z $fh){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$@ or -z $fh
MC/DC100%2/2
$@ or -z $fh
$@
-z $fh
Truth table100%3/3
A or B
A= $@B= -z $fh
| |||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | # we've got an empty file... | ||||||||||||||||||||||||||||
| 266 | # we'll set recsep to the local platform's | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 9 | $self->{recsep} = $self->platform_recsep; | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | return $want | ||||||||||||||||||||||||||||
| 271 | ? $self->_convert_recsep($self->{recsep}, $want) | ||||||||||||||||||||||||||||
| 272 | 9 | ▶ | : $self->{recsep}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 217 | seek $fh, 0, 0; | |||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 217 | my $recsep_regex = $self->_recsep_regex; | |||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 217 | ▶ | if (<$fh> =~ /$recsep_regex/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 217 | $self->{recsep} = $1; | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 217 | ▶ | close $fh or confess "recsep() can't close file $file!: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | return $want | ||||||||||||||||||||||||||||
| 286 | ? $self->_convert_recsep($self->{recsep}, $want) | ||||||||||||||||||||||||||||
| 287 | 217 | ▶ | : $self->{recsep}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | sub platform_recsep { | ||||||||||||||||||||||||||||
| 290 | 183 | ▶ | my $self = ref $_[0] eq __PACKAGE__ | ||||||||||||||||||||||||||
|
Subroutine
platform_recsep: called CC=8 SCAR=21.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | ? shift | ||||||||||||||||||||||||||||
| 292 | : __PACKAGE__->new; | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 183 | my $want = shift; | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # for platform_recsep(), we need the file open in ASCII mode, | ||||||||||||||||||||||||||||
| 297 | # so we can't use _binmode_handle() or File::Temp | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 183 | my $file = $self->_temp_filename; | |||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 183 | ▶ | open my $wfh, '>', $file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | or confess | ||||||||||||||||||||||||||||
| 303 | "platform_recsep() can't open temp file $file for writing!: $!"; | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 183 | print $wfh "abc\n"; | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 183 | ▶ | close $wfh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | or confess "platform_recsep() can't close write temp file $file: $!"; | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 183 | my $fh = $self->_binmode_handle($file); | |||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 183 | my $recsep_regex = $self->_recsep_regex; | |||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 183 | ▶ | if (<$fh> =~ /$recsep_regex/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 183 | $self->{platform_recsep} = $1; | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 183 | ▶ | close $fh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 319 | or confess "platform_recsep() can't close temp file $file after run: $!"; | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 183 | ▶ | unlink $file or confess "Can't unlink temp file '$file': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | return $want | ||||||||||||||||||||||||||||
| 324 | ? $self->_convert_recsep($self->{platform_recsep}, $want) | ||||||||||||||||||||||||||||
| 325 | 183 | ▶ | : $self->{platform_recsep}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | sub tempfile { | ||||||||||||||||||||||||||||
| 328 | # returns a temporary file handle in write mode | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 41 | ▶ | my $wfh = File::Temp->new(UNLINK => 1); | ||||||||||||||||||||||||||
|
Subroutine
tempfile: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 331 | 41 | return $wfh; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | sub _config { | ||||||||||||||||||||||||||||
| 334 | # configures self with incoming params | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 223 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_config: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 337 | 223 | my %p = @_; | |||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 223 | $self->{custom_recsep} = $p{recsep}; | |||||||||||||||||||||||||||
| 340 | 223 | delete $p{recsep}; | |||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 223 | my @params = qw( | |||||||||||||||||||||||||||
| 343 | testing copy types list maxdepth | ||||||||||||||||||||||||||||
| 344 | insert line find limit | ||||||||||||||||||||||||||||
| 345 | ); | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 223 | for (@params){ | |||||||||||||||||||||||||||
| 348 | 2007 | delete $self->{$_}; | |||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 223 | for (keys %p){ | |||||||||||||||||||||||||||
| 352 | 489 | $self->{$_} = $p{$_}; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | sub _handle { | ||||||||||||||||||||||||||||
| 356 | # returns a handle with platform's record separator | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 54 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_handle: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 359 | 54 | my $file = shift; | |||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 54 | my $fh; | |||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 54 | ▶ | if ($self->recsep($file, 'hex') ne $self->platform_recsep('hex')){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 16 | $fh = $self->_binmode_handle($file); | |||||||||||||||||||||||||||
| 366 | 16 | my $temp_wfh = $self->tempfile; | |||||||||||||||||||||||||||
| 367 | 16 | binmode $temp_wfh, ':raw'; | |||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 16 | my $temp_filename = $temp_wfh->filename; | |||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 16 | ▶ | while (<$fh>){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | 92 | $_ = $self->_platform_replace($_); | |||||||||||||||||||||||||||
| 373 | 92 | print $temp_wfh $_; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 16 | ▶ | close $fh or confess "can't close file $file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | 16 | ▶ | close $temp_wfh or confess "can't close file $temp_filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | 16 | my $ret_fh = $self->_binmode_handle($temp_filename); | |||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 16 | return $ret_fh; | |||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | else { | ||||||||||||||||||||||||||||
| 384 | 38 | $fh = $self->_binmode_handle($file); | |||||||||||||||||||||||||||
| 385 | 38 | return $fh; | |||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | sub _binmode_handle { | ||||||||||||||||||||||||||||
| 389 | # returns a handle opened with binmode :raw | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 608 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_binmode_handle: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 392 | 608 | my $file = shift; | |||||||||||||||||||||||||||
| 393 | 608 | ▶ | my $mode = shift || 'r'; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 'r'
MC/DC100%1/1
shift() || 'r'
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 608 | my $fh; | |||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 608 | ▶ | if ($mode =~ /^w/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 398 | 71 | ▶ | open $fh, '>', $file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 399 | or confess "_binmode_handle() can't open file $file for writing!: $!"; | ||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | else { | ||||||||||||||||||||||||||||
| 402 | 537 | ▶ | open $fh, '<', $file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 403 | or confess "_binmode_handle() can't open file $file for reading!: $!"; | ||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 601 | binmode $fh, ':raw'; | |||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 601 | return $fh; | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | sub _convert_recsep { | ||||||||||||||||||||||||||||
| 411 | # converts recsep to either hex or OS name (ie. type) | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | 175 | ▶ | my ($self, $sep, $want) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_convert_recsep: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | 175 | $sep = unpack "H*", $sep; | |||||||||||||||||||||||||||
| 416 | 175 | $sep =~ s/0/\\0/g; | |||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 175 | ▶ | return $sep if $want eq 'hex'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 23 | my %seps = ( | |||||||||||||||||||||||||||
| 421 | '\0a' => 'nix', | ||||||||||||||||||||||||||||
| 422 | '\0d\0a' => 'win', | ||||||||||||||||||||||||||||
| 423 | '\0d' => 'mac', | ||||||||||||||||||||||||||||
| 424 | ); | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 23 | ▶ | return $seps{$sep} || 'unknown'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$seps{$sep} || 'unknown'
MC/DC100%1/1
$seps{$sep} || 'unknown'
$seps{$sep}
Truth table100%2/2
A
A= $seps{$sep}
| |||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | sub _recsep_regex { | ||||||||||||||||||||||||||||
| 429 | # returns a regex object representing all recseps | ||||||||||||||||||||||||||||
| 430 | 1437 | ▶ | return qr/([\n\x{0B}\f\r\x{85}]{1,2})/; | ||||||||||||||||||||||||||
|
Subroutine
_recsep_regex: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||
| 432 | sub _platform_replace { | ||||||||||||||||||||||||||||
| 433 | # replace recseps in a string with the platform recsep | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 92 | ▶ | my ($self, $str) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_platform_replace: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 436 | 92 | my $re = $self->_recsep_regex; | |||||||||||||||||||||||||||
| 437 | 92 | $str =~ s/$re/$self->platform_recsep/ge; | |||||||||||||||||||||||||||
| 438 | 92 | return $str; | |||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||
| 440 | sub _strip_ends { | ||||||||||||||||||||||||||||
| 441 | # strip all line endings from string | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | 945 | ▶ | my ($self, $str) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_strip_ends: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 444 | 945 | my $re = $self->_recsep_regex; | |||||||||||||||||||||||||||
| 445 | 945 | $str =~ s/$re//g; | |||||||||||||||||||||||||||
| 446 | 945 | return $str; | |||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||
| 448 | sub _temp_filename { | ||||||||||||||||||||||||||||
| 449 | # return the name of a temporary file | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | 186 | ▶ | my $temp_fh = File::Temp->new(UNLINK => 1); | ||||||||||||||||||||||||||
|
Subroutine
_temp_filename: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 452 | 186 | my $filename = $temp_fh->filename; | |||||||||||||||||||||||||||
| 453 | 186 | return $filename; | |||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||
| 455 | 1 | ▶ | sub _vim_placeholder { return 1; }; # for folding | ||||||||||||||||||||||||||
|
Subroutine
_vim_placeholder: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 17 | 1; | |||||||||||||||||||||||||||
| 458 | __END__ | ||||||||||||||||||||||||||||