| 1 | 1 | ▶ | use v5.12; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Encode::Guess; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | package Kephra::IO::LocalFile; | ||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | 0 | ▶ | sub dir_from_path { | ||||||||||||||||||||||||||||||||||
|
Subroutine
dir_from_path: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 11 | 0 | my $path = shift; | |||||||||||||||||||||||||||||||||||
| 12 | 0 | ▶ | return unless defined $path; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 13 | 0 | my ($volume, $directories, $file) = File::Spec->splitpath( $path ); | |||||||||||||||||||||||||||||||||||
| 14 | 0 | File::Spec->catdir( $volume, $directories ); | |||||||||||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub normalize_path { | ||||||||||||||||||||||||||||||||||
|
Subroutine
normalize_path: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 18 | 0 | my $path = shift; | |||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | return unless defined $path and $path; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $path and $path
MC/DC0%0/2
defined $path and $path
defined $path
$path
Truth table0%0/3
A and B
A= defined $pathB= $path
| |||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||
| 21 | 0 | $path = File::Spec->canonpath( $path ); | |||||||||||||||||||||||||||||||||||
| 22 | 0 | local $/ = "\r\n"; | |||||||||||||||||||||||||||||||||||
| 23 | 0 | chomp( $path ); | |||||||||||||||||||||||||||||||||||
| 24 | 0 | return $path; | |||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub read { | ||||||||||||||||||||||||||||||||||
|
Subroutine
read: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 28 | 0 | my $path = normalize_path( shift ); | |||||||||||||||||||||||||||||||||||
| 29 | 0 | my $encoding; | |||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | return warning("can't load nonexising file") unless defined $path and -e $path; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $path and -e $path
MC/DC0%0/2
defined $path and -e $path
defined $path
-e $path
Truth table0%0/3
A and B
A= defined $pathB= -e $path
| |||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | return warning("can't read $path") unless -r $path; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 32 | 0 | open my $FH, '<', $path; | |||||||||||||||||||||||||||||||||||
| 33 | 0 | binmode($FH); | |||||||||||||||||||||||||||||||||||
| 34 | 0 | my $content = do { local $/; <$FH> }; | |||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ($content) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 36 | 0 | my @guesses = qw/utf-8 iso8859-1 latin1/; | |||||||||||||||||||||||||||||||||||
| 37 | 0 | my $guess = Encode::Guess::guess_encoding( $content, @guesses ); | |||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ( ref($guess) and ref($guess) =~ m/^Encode::/ ) { $encoding = $guess->name } | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/3
ref $guess and ref($guess) =~ /^Encode::/u
MC/DC0%0/2
ref $guess and ref($guess) =~ /^Encode::/u
ref $guess
ref($guess) =~ /^Encode::/u
Truth table0%0/3
A and B
A= ref $guessB= ref($guess) =~ /^Encode::/u
| |||||||||||||||||||||||||||||||||||||
| 39 | 0 | elsif ( $guess =~ m/utf8/ ) { $encoding = 'utf-8' } | |||||||||||||||||||||||||||||||||||
| 40 | elsif ( $guess =~ m/or/ ) { | ||||||||||||||||||||||||||||||||||||
| 41 | 0 | my @suggest_encodings = split /\sor\s/, "$guess"; | |||||||||||||||||||||||||||||||||||
| 42 | 0 | $encoding = $suggest_encodings[0]; | |||||||||||||||||||||||||||||||||||
| 43 | 0 | } else { $encoding = 'utf-8' } | |||||||||||||||||||||||||||||||||||
| 44 | 0 | $content = Encode::decode( $encoding, $content ); | |||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||
| 46 | 0 | return $content, $encoding; | |||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub write { | ||||||||||||||||||||||||||||||||||
|
Subroutine
write: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 50 | 0 | my ($path, $encoding, $content) = @_; | |||||||||||||||||||||||||||||||||||
| 51 | 0 | $path = normalize_path( $path ); | |||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $encoding = 'utf-8' unless defined $encoding; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | return say "need a file path to write into" unless $path; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | return say "can't overwrite $path" if -e $path and not -w $path; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $path and not -w $path
MC/DC0%0/2
-e $path and not -w $path
-e $path
not -w $path
Truth table0%0/3
A and not(B)
A= -e $pathB= not -w $path
| |||||||||||||||||||||||||||||||||||||
| 55 | 0 | open my $FH, "> :raw :encoding($encoding)", $path; | |||||||||||||||||||||||||||||||||||
| 56 | 0 | print $FH $content; | |||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||
| 60 | 1 | 1; | |||||||||||||||||||||||||||||||||||