lib/App/Bernard/Magic/Single.pm

filter: Statementstmt 66.6% 32 / 48 Branchbran 20.0% 4 / 20 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 77.7% 7 / 9 Podpod 0.0% 0 / 4 totaltotal 47.3% 44 / 93 CC 13 SCAR 32.6
CC
13
Cov
49%
handle
46.9
replace_inplace
6.0
__ANON__
6.0
CRAP 26.0
« blib/lib/App/Bernard/Filetype/TextXGettextTranslation.pm ↑ Summary lib/App/Bernard.pm »
1 package App::Bernard::Magic::Single;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::MimeInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
9 1 return bless {};
10 }
11  
12 1 ▶sub package_from_filetype {
Subroutine package_from_filetype: called CC=1 SCAR=0.0
Pod undocumented
13 1 my ($filename) = @_;
14  
15 1 my $mimetype = mimetype($filename);
16
17 1 my $pkg = $mimetype;
18 1 $pkg =~ s/\b(.)/\U$1/g;
19 1 $pkg =~ s/[^a-z]//gi;
20  
21 1 return ("App::Bernard::Filetype::$pkg", $mimetype);
22 }
23  
24 0 ▶sub replace_inplace {
Subroutine replace_inplace: not called CC=2 SCAR=17.9
Pod undocumented
25  
26 0 my ($settings) = @_;
27  
28 0 ▶ if ($settings->{'inplace'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{"inplace"}) 0 0 0
29 0 rename $settings->{'output'}, $settings->{'input'};
30 }
31 }
32  
33 1 ▶sub handle {
Subroutine handle: called CC=11 SCAR=38.5
Pod undocumented
34  
35 1 my ($self, $settings) = @_;
36  
37 1 ▶ if ($settings->{'output'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{"output"}) 0 1 1
38 0 ▶ open OUTPUT, ">$settings->{'output'}"
Branch: 1 branch
BranchTrueFalseTotal
open OUTPUT, qq[>$settings->{"output"}] or die qq[Can't open $settings->{"output"}: $!] 0 0 0
39 or die "Can't open $settings->{'output'}: $!";
40 0 binmode OUTPUT, ":utf8";
41 }
42  
43 0 ▶ $settings->{'print'} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
44 0 my ($text) = @_;
45  
46 0 ▶ if ($settings->{'output'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{'output'}) { } 0 0 0
47 0 print OUTPUT $text;
48 } else {
49 0 print $text;
50 }
51 1 };
52  
53 1 ▶ die "File $settings->{'input'} does not exist\n" unless -e $settings->{'input'};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $settings->{"input"} 0 1 1
54 1 my ($pkg, $mimetype) = package_from_filetype($settings->{'input'});
55  
56 # TODO: Probably should query the package
57 # about whether it accepts --in-place
58 die "--in-place can only be used with .po files"
59 1 ▶ if $settings->{'inplace'} && $mimetype ne 'text/x-gettext-translation';
Branch: 1 branch
BranchTrueFalseTotal
if $settings->{"inplace"} and $mimetype ne "text/x-gettext-translation" 0 1 1
Condition33%1/3
$settings->{"inplace"} and $mimetype ne "text/x-gettext-translation"
!ll&&!rl&&r
100
MC/DC0%0/2
$settings->{"inplace"} and $mimetype ne "text/x-gettext-translation"
$settings->{"inplace"} $mimetype ne "text/x-gettext-translation"
Truth table33%1/3
A and B
A= $settings->{"inplace"}B= $mimetype ne "text/x-gettext-translation"
ABresult
0X0
100
111
60  
61 1 eval {
62 # okay, attempt to load the handler package
63 1 my $filename = $pkg;
64 1 $filename =~ s!::!/!g;
65 1 require "$filename.pm";
66 };
67 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
68 1 die "$settings->{'file'} is of type $mimetype, which we cannot handle.\n";
69 }
70  
71 0 $pkg->handle(scalar(read_file($settings->{'input'})),
72 $settings);
73  
74 close OUTPUT
75 or die "Can't close $settings->{'output'}: $!"
76 0 ▶ if $settings->{'output'};
Branch: 1 branch
BranchTrueFalseTotal
if $settings->{"output"} 0 0 0
Condition0%0/5
$settings->{'output'} && (close OUTPUT || die("Can't close $settings->{'output'}: $!"))
!ll&&!rl&&r
000
close OUTPUT or die qq[Can't close $settings->{"output"}: $!]
l!l
00
MC/DC0%0/2
$settings->{'output'} && (close OUTPUT || die("Can't close $settings->{'output'}: $!"))
$settings->{'output'} close OUTPUT
Truth table0%0/3
A && B
A= $settings->{'output'}B= close OUTPUT
ABresult
0X0
100
111
77  
78 0 ▶ if ($settings->{'check'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{"check"}) 0 0 0
79 0 my $result =
80 system "msgfmt -c $settings->{'output'} -o /dev/null";
81  
82 0 ▶ if ($result != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result != 0) 0 0 0
83 0 die "Check failed; aborting\n";
84 }
85 }
86  
87 0 replace_inplace($settings);
88 }
89  
90 1 1;
« blib/lib/App/Bernard/Filetype/TextXGettextTranslation.pm ↑ Summary lib/App/Bernard.pm »