blib/lib/File/Blarf.pm

filter: Statementstmt 59.4% 41 / 69 Branchbran 34.0% 17 / 50 Conditioncond 55.5% 5 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 85.7% 6 / 7 Podpod 100% 3 / 3 totaltotal 51.7% 74 / 143 CC 30 SCAR 42.8
CC
30
Cov
49%
cat
182.0
blarf
14.0
slurp
9.8
CRAP 72.2
↑ Summary
1 package File::Blarf;
2 {
3 3 $File::Blarf::VERSION = '0.12';
4 }
5 # ABSTRACT: Simple reading and writing of files
6  
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Fcntl qw( :flock );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 3 ▶sub slurp {
Subroutine slurp: called CC=9 SCAR=22.8
Pod documented
15 3 my $file = shift;
16 3 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
21
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
17  
18 3 ▶ if ( -e $file && open( my $FH, '<', $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and open my $FH, '<', $file) { } 3 0 3
Condition33%1/3
-e $file and open my $FH, "<", $file
!ll&&!rl&&r
003
MC/DC0%0/2
-e $file and open my $FH, "<", $file
-e $file open my $FH, "<", $file
Truth table33%1/3
A and B
A= -e $fileB= open my $FH, "<", $file
ABresult
0X0
100
111
19 3 ▶ flock( $FH, LOCK_SH ) if $opts->{Flock};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"Flock"} 0 3 3
20 3 my @lines = <$FH>;
21 3 ▶ flock( $FH, LOCK_UN ) if $opts->{Flock};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"Flock"} 0 3 3
22 # DGR: we just read it, what could possibly go wrong?
23 ## no critic (RequireCheckedClose)
24 3 close($FH);
25 ## use critic
26 3 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 2 3
27 1 ▶ if ( $opts->{Chomp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"Chomp"}) 1 0 1
28 1 chomp(@lines);
29 }
30 1 return @lines;
31 }
32 else {
33 2 my $out = join q{}, @lines;
34 2 ▶ if ( $opts->{Chomp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"Chomp"}) 1 1 2
35 1 chomp($out);
36 }
37 2 return $out;
38 }
39 }
40 else {
41 0 return;
42 }
43 }
44  
45  
46 5 ▶sub blarf {
Subroutine blarf: called CC=10 SCAR=26.4
Pod documented
47 5 my $file = shift;
48 5 my $str = shift;
49 5 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
23
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
50  
51 5 my $mode = '>';
52 5 ▶ if ( $opts->{Append} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"Append"}) 0 5 5
53 0 $mode = '>>';
54 }
55 # DGR: due to flock and newlines we can't be anymore brief
56 ## no critic (RequireBriefOpen)
57 5 ▶ if ( open( my $FH, $mode, $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $FH, $mode, $file) 5 0 5
58 5 ▶ flock( $FH, LOCK_EX ) if $opts->{Flock};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"Flock"} 2 3 5
59 4 ▶ if(!print $FH $str) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $FH $str) 0 4 4
60 0 return;
61 }
62 4 ▶ if ( $opts->{'Newline'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"Newline"}) 0 4 4
63 0 ▶ if(!print $FH "\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $FH "\n") 0 0 0
64 0 return;
65 }
66 }
67 4 ▶ flock( $FH, LOCK_UN ) if $opts->{Flock};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"Flock"} 1 3 4
68 4 ▶ if(close($FH)) {
Branch: 1 branch
BranchTrueFalseTotal
if (close $FH) 4 0 4
69 4 return 1;
70 }
71 }
72 ## use critic
73  
74 0 return;
75 }
76  
77 0 ▶sub cat {
Subroutine cat: not called CC=13 SCAR=52.0
Pod documented
78 0 my $source_file = shift;
79 0 my $dest_file = shift;
80 0 ▶ my $opts = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
81  
82 0 my $mode = '>';
83 0 ▶ if ( $opts->{Append} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"Append"}) 0 0 0
84 0 $mode = '>>';
85 }
86 # DGR: the files could be huge, so we MUST no read them into main memory
87 ## no critic (RequireBriefOpen)
88 0 ▶ if ( open( my $IN, '<', $source_file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $IN, "<", $source_file) 0 0 0
89 0 ▶ flock( $IN, LOCK_SH ) if $opts->{Flock};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"Flock"} 0 0 0
90 0 my $status = 0;
91 0 ▶ if ( open( my $OUT, $mode, $dest_file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $OUT, $mode, $dest_file) 0 0 0
92 0 ▶ flock( $OUT, LOCK_EX ) if $opts->{Flock};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"Flock"} 0 0 0
93 0 ▶ while ( my $line = <$IN> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $IN)) 0 0 0
94 0 ▶ if(!print $OUT $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $OUT $line) 0 0 0
95 0 return;
96 }
97 }
98 0 ▶ flock( $OUT, LOCK_UN ) if $opts->{Flock};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"Flock"} 0 0 0
99 0 ▶ if(close($OUT)) {
Branch: 1 branch
BranchTrueFalseTotal
if (close $OUT) 0 0 0
100 0 $status = 1;
101 }
102 }
103 0 ▶ flock( $IN, LOCK_UN ) if $opts->{Flock};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"Flock"} 0 0 0
104 # DGR: we were just reading ...
105 ## no critic (RequireCheckedClose)
106 0 close($IN);
107 ## use critic
108 0 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) 0 0 0
109 0 return $status;
110 }
111 }
112 ## use critic
113 # something went wrong ...
114 0 return;
115 }
116  
117  
118 3 1; # End of File::Blarf
119  
120 __END__
↑ Summary