line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::Blarf; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$File::Blarf::VERSION = '0.12'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
# ABSTRACT: Simple reading and writing of files |
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
77948
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
73
|
|
8
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
60
|
|
9
|
2
|
|
|
2
|
|
43
|
use 5.008; |
|
2
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
83
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
12
|
use Fcntl qw( :flock ); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1672
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub slurp { |
15
|
3
|
|
|
3
|
1
|
5
|
my $file = shift; |
16
|
3
|
|
100
|
|
|
13
|
my $opts = shift || {}; |
17
|
|
|
|
|
|
|
|
18
|
3
|
50
|
33
|
|
|
250
|
if ( -e $file && open( my $FH, '<', $file ) ) { |
19
|
3
|
50
|
|
|
|
9
|
flock( $FH, LOCK_SH ) if $opts->{Flock}; |
20
|
3
|
|
|
|
|
53
|
my @lines = <$FH>; |
21
|
3
|
50
|
|
|
|
22
|
flock( $FH, LOCK_UN ) if $opts->{Flock}; |
22
|
|
|
|
|
|
|
# DGR: we just read it, what could possibly go wrong? |
23
|
|
|
|
|
|
|
## no critic (RequireCheckedClose) |
24
|
3
|
|
|
|
|
27
|
close($FH); |
25
|
|
|
|
|
|
|
## use critic |
26
|
3
|
100
|
|
|
|
8
|
if (wantarray) { |
27
|
1
|
50
|
|
|
|
4
|
if ( $opts->{Chomp} ) { |
28
|
1
|
|
|
|
|
3
|
chomp(@lines); |
29
|
|
|
|
|
|
|
} |
30
|
1
|
|
|
|
|
8
|
return @lines; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
else { |
33
|
2
|
|
|
|
|
5
|
my $out = join q{}, @lines; |
34
|
2
|
100
|
|
|
|
6
|
if ( $opts->{Chomp} ) { |
35
|
1
|
|
|
|
|
3
|
chomp($out); |
36
|
|
|
|
|
|
|
} |
37
|
2
|
|
|
|
|
15
|
return $out; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
else { |
41
|
0
|
|
|
|
|
0
|
return; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub blarf { |
47
|
5
|
|
|
5
|
1
|
1240
|
my $file = shift; |
48
|
5
|
|
|
|
|
10
|
my $str = shift; |
49
|
5
|
|
100
|
|
|
22
|
my $opts = shift || {}; |
50
|
|
|
|
|
|
|
|
51
|
5
|
|
|
|
|
9
|
my $mode = '>'; |
52
|
5
|
50
|
|
|
|
15
|
if ( $opts->{Append} ) { |
53
|
0
|
|
|
|
|
0
|
$mode = '>>'; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
# DGR: due to flock and newlines we can't be anymore brief |
56
|
|
|
|
|
|
|
## no critic (RequireBriefOpen) |
57
|
5
|
50
|
|
|
|
421
|
if ( open( my $FH, $mode, $file ) ) { |
58
|
5
|
100
|
|
|
|
999998
|
flock( $FH, LOCK_EX ) if $opts->{Flock}; |
59
|
5
|
50
|
|
|
|
95
|
if(!print $FH $str) { |
60
|
0
|
|
|
|
|
0
|
return; |
61
|
|
|
|
|
|
|
} |
62
|
4
|
50
|
|
|
|
102
|
if ( $opts->{'Newline'} ) { |
63
|
0
|
0
|
|
|
|
0
|
if(!print $FH "\n") { |
64
|
0
|
|
|
|
|
0
|
return; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
4
|
100
|
|
|
|
64
|
flock( $FH, LOCK_UN ) if $opts->{Flock}; |
68
|
4
|
50
|
|
|
|
125
|
if(close($FH)) { |
69
|
4
|
|
|
|
|
43
|
return 1; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
## use critic |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
return; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub cat { |
78
|
0
|
|
|
0
|
1
|
|
my $source_file = shift; |
79
|
0
|
|
|
|
|
|
my $dest_file = shift; |
80
|
0
|
|
0
|
|
|
|
my $opts = shift || {}; |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
my $mode = '>'; |
83
|
0
|
0
|
|
|
|
|
if ( $opts->{Append} ) { |
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
|
0
|
|
|
|
|
if ( open( my $IN, '<', $source_file ) ) { |
89
|
0
|
0
|
|
|
|
|
flock( $IN, LOCK_SH ) if $opts->{Flock}; |
90
|
0
|
|
|
|
|
|
my $status = 0; |
91
|
0
|
0
|
|
|
|
|
if ( open( my $OUT, $mode, $dest_file ) ) { |
92
|
0
|
0
|
|
|
|
|
flock( $OUT, LOCK_EX ) if $opts->{Flock}; |
93
|
0
|
|
|
|
|
|
while ( my $line = <$IN> ) { |
94
|
0
|
0
|
|
|
|
|
if(!print $OUT $line) { |
95
|
0
|
|
|
|
|
|
return; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
0
|
0
|
|
|
|
|
flock( $OUT, LOCK_UN ) if $opts->{Flock}; |
99
|
0
|
0
|
|
|
|
|
if(close($OUT)) { |
100
|
0
|
|
|
|
|
|
$status = 1; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
0
|
0
|
|
|
|
|
flock( $IN, LOCK_UN ) if $opts->{Flock}; |
104
|
|
|
|
|
|
|
# DGR: we were just reading ... |
105
|
|
|
|
|
|
|
## no critic (RequireCheckedClose) |
106
|
0
|
|
|
|
|
|
close($IN); |
107
|
|
|
|
|
|
|
## use critic |
108
|
0
|
0
|
|
|
|
|
if ($status) { |
109
|
0
|
|
|
|
|
|
return $status; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
## use critic |
113
|
|
|
|
|
|
|
# something went wrong ... |
114
|
0
|
|
|
|
|
|
return; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; # End of File::Blarf |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
__END__ |