PTP/Files.pm

filter: Statementstmt 23.0% 21 / 91 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 31.2% 5 / 16 Podpod 0.0% 0 / 11 totaltotal 13.9% 26 / 187 CC 33 SCAR 33.9
CC
33
Cov
13%
read_handle
72.0
init_global_output
56.0
write_file
30.0
CRAP 29.7
« PTP/Commands.pm ↑ Summary PTP/Args.pm »
1 # This module provides functions to read the input files and to write the
2 # resulting output.
3  
4 package App::PTP::Files;
5  
6 2 ▶use 5.022;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '0.01';
14  
15 # Every public function used by the main code is exported by default.
16 2 our @EXPORT = qw(init_global_output close_global_output read_input write_output);
17 2 our @EXPORT_OK = qw(write_side_output read_side_input write_handle);
18  
19 # The reference to this variable is used in the input list to specify that the
20 # standard input should be read.
21 2 our $stdin_marker = '<STDIN>';
22  
23 # The reference to this variable is used in the input list to specify that all
24 # the inputs have been merged.
25 2 our $merged_marker = '<merged input>';
26  
27 2 my $global_output_fh;
28  
29 # $stdout is where the default (non-debug) output of the program should go. It
30 # will always be STDOUT, except during the tests.
31 0 ▶sub init_global_output {
Subroutine init_global_output: not called CC=7 SCAR=40.3
Pod undocumented
32 0 my ($options, $stdout) = @_;
33 0 ▶ if ($options->{output}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options->{'output'}) { } 0 0 0
elsif (not $options->{'in_place'}) { } 0 0 0
34 0 ▶ if ($options->{debug_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"debug_mode"}) 0 0 0
35 0 print "All output is going to: $options->{output}\n";
36 }
37 0 ▶ my $mode = $options->{append} ? '>>' : '>';
Branch: 1 branch
BranchTrueFalseTotal
$options->{'append'} ? : 0 0 0
38 open($global_output_fh, "${mode}:encoding($options->{output_encoding})", $options->{output})
39 0 ▶ or die "Cannot open output file '$options->{output}': $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
open $global_output_fh, qq[${mode}:encoding($options->{"output_encoding"})], $options->{"output"} or die qq[Cannot open output file '$options->{"output"}': $!.\n] 0 0 0
40 } elsif (not $options->{in_place}) {
41 0 ▶ print "All output is going to STDOUT.\n" if $options->{debug_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"debug_mode"} 0 0 0
42 0 $global_output_fh = $stdout;
43 }
44 # We're setting the correct binmode for STDOUT here, because it can be used
45 # when in_place is true, but also if the --tee command is used.
46 0 binmode($stdout, ":encoding($options->{output_encoding})");
47 }
48  
49 0 ▶sub close_global_output {
Subroutine close_global_output: not called CC=3 SCAR=24.8
Pod undocumented
50 0 my ($options) = @_;
51 0 ▶ if ($options->{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"output"}) 0 0 0
52 0 ▶ close $global_output_fh
Branch: 1 branch
BranchTrueFalseTotal
close $global_output_fh or die qq[Cannot close output file '$options->{"output"}': $!.\n] 0 0 0
53 or die "Cannot close output file '$options->{output}': $!.\n";
54 }
55 }
56  
57 # read_handle(handle)
58 # Reads the content of the given handle and returns an array ref containing two
59 # elements. The first one is an array-ref with all the lines of the file and the
60 # second one is a variable indicating if the last line of the file had a final
61 # separator.
62 # This method uses the value of the `$input_separator` global option.
63 0 ▶sub read_handle {
Subroutine read_handle: not called CC=8 SCAR=42.8
Pod undocumented
64 0 my ($handle, $options) = @_;
65 0 local $/ = undef; # enable slurp mode;
66 0 my $content = <$handle>;
67 0 ▶ if (not defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 0 0 0
68 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
69 0 chomp($@);
70 0 die "FATAL: Cannot read input: $@\n";
71 }
72 # Theoretically this should not happen. But, on 5.22 this seems to happens
73 # if the input file is empty.
74 0 $content = '';
75 }
76 0 my @content;
77 0 ▶ if ($options->{preserve_eol}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'preserve_eol'}) { } 0 0 0
78 0 @content = $content =~ /\G ( .*? (?n: $options->{input_separator} ))/xgcms;
79 } else {
80 0 @content = $content =~ /\G (.*?) (?n: $options->{input_separator} )/xgcms;
81 }
82 0 my $missing_final_separator = 0;
83 0 ▶ if ((pos($content) // 0) < length($content)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((pos $content // 0) < length $content) 0 0 0
Condition0%0/2
pos $content // 0
l!l
00
MC/DC0%0/1
pos $content // 0
pos $content
Truth table0%0/2
A
A= pos $content
Aresult
11
00
84 0 $missing_final_separator = 1;
85 0 ▶ print "The last line has no separator.\n" if $options->{debug_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"debug_mode"} 0 0 0
86 0 ▶ push @content, substr($content, pos($content) // 0);
Condition0%0/2
pos $content // 0
l!l
00
MC/DC0%0/1
pos $content // 0
pos $content
Truth table0%0/2
A
A= pos $content
Aresult
11
00
87 }
88 0 return (\@content, $missing_final_separator);
89 }
90  
91 # read_file(path)
92 # Opens the given file, applies the correct read option, and calls read_handle.
93 0 ▶sub read_file {
Subroutine read_file: not called CC=4 SCAR=30.0
Pod undocumented
94 0 my ($path, $options) = @_;
95 0 ▶ print "Reading file: ${path}\n" if $options->{debug_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"debug_mode"} 0 0 0
96 0 ▶ open(my $fh, "<:encoding($options->{input_encoding})", $path)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, qq[<:encoding($options->{"input_encoding"})], $path or die "Cannot open file '${path}': $!.\n" 0 0 0
97 or die "Cannot open file '$path': $!.\n";
98 0 my @data = read_handle($fh, $options);
99 0 ▶ close($fh) or die "Cannot close the file '$path': $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close the file '${path}': $!.\n" 0 0 0
100 0 return @data;
101 }
102  
103 # read_stdin()
104 # Applies the correct read option to STDIN and calls read_handle(STDIN).
105 0 ▶sub read_stdin {
Subroutine read_stdin: not called CC=2 SCAR=17.9
Pod undocumented
106 0 my ($options, $stdin) = @_;
107 0 ▶ print "Reading STDIN\n" if $options->{debug_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"debug_mode"} 0 0 0
108 0 binmode($stdin, ":encoding($options->{input_encoding})");
109 0 return read_handle($stdin, $options);
110 }
111  
112 # read_input($input, $options, \*STDIN)
113 # Checks whether the input is the $stdin_marker or a file name and calls the
114 # matching method to read it, the third argument is the file-handle to read
115 # when $input is the $stdin_marker (usually STDIN except in tests).
116 0 ▶sub read_input {
Subroutine read_input: not called CC=3 SCAR=24.8
Pod undocumented
117 0 my ($input, $options, $stdin) = @_;
118 0 ▶ if (ref($input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input) 0 0 0
119 0 ▶ if ($input == \$stdin_marker) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input == \$App::PTP::Files::stdin_marker) { } 0 0 0
120 0 return read_stdin($options, $stdin);
121 } else {
122 0 die 'Should not happen ('.Dumper($input).")\n";
123 }
124 }
125 0 return read_file($input, $options);
126 }
127  
128 # write_content($handle, \@content, $missing_final_separator, \%options)
129 0 ▶sub write_handle {
Subroutine write_handle: not called CC=4 SCAR=30.0
Pod undocumented
130 0 my ($handle, $content, $missing_final_separator, $options) = @_;
131 0 ▶ return unless @$content;
Branch: 1 branch
BranchTrueFalseTotal
unless @$content 0 0 0
132 0 local $, = $options->{output_separator};
133 local $\ = $options->{output_separator}
134 0 ▶ if $options->{fix_final_separator} || !$missing_final_separator;
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"fix_final_separator"} or not $missing_final_separator 0 0 0
Condition0%0/3
$options->{"fix_final_separator"} or not $missing_final_separator
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{"fix_final_separator"} or not $missing_final_separator
$options->{"fix_final_separator"} not $missing_final_separator
Truth table0%0/3
A or not(B)
A= $options->{"fix_final_separator"}B= not $missing_final_separator
ABresult
1X1
011
000
135 0 print $handle @$content;
136 }
137  
138 # write_file($output_file_name, \@content, $missing_final_separator, append,
139 # \%options)
140 0 ▶sub write_file {
Subroutine write_file: not called CC=5 SCAR=34.0
Pod undocumented
141 0 my ($file_name, $content, $missing_final_separator, $append, $options) = @_;
142 0 ▶ my $m = $append ? '>>' : '>';
Branch: 1 branch
BranchTrueFalseTotal
$append ? : 0 0 0
143 0 ▶ print "Outputting result to: ${m}${file_name}\n" if $options->{debug_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"debug_mode"} 0 0 0
144 0 ▶ open(my $out_fh, "${m}:encoding($options->{output_encoding})", $file_name)
Branch: 1 branch
BranchTrueFalseTotal
open my $out_fh, qq[${m}:encoding($options->{"output_encoding"})], $file_name or die "Cannot open output file '${file_name}': $!.\n" 0 0 0
145 or die "Cannot open output file '${file_name}': $!.\n";
146 0 write_handle($out_fh, $content, $missing_final_separator, $options);
147 0 ▶ close $out_fh or die "Cannot close output file '${file_name}': $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
close $out_fh or die "Cannot close output file '${file_name}': $!.\n" 0 0 0
148 }
149  
150 # write_file($input_file_name, \@content, $missing_final_separator, \%options)
151 0 ▶sub write_output {
Subroutine write_output: not called CC=2 SCAR=17.9
Pod undocumented
152 0 my ($file_name, $content, $missing_final_separator, $options) = @_;
153 0 ▶ if ($options->{in_place}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'in_place'}) { } 0 0 0
154 0 write_file($file_name, $content, $missing_final_separator, 0, $options);
155 } else {
156 0 write_handle($global_output_fh, $content, $missing_final_separator, $options);
157 }
158 }
159  
160 # These two methods are used by commands which read or write to side input/
161 # output files. The difference is that they expect a '-' in the given filename
162 # instead of the #stdin_marker, when referring to the standard input (or
163 # output).
164 my %known_side_output;
165  
166 0 ▶sub write_side_output {
Subroutine write_side_output: not called CC=3 SCAR=24.8
Pod undocumented
167 0 my ($file_name, $content, $missing_final_separator, $options) = @_;
168 0 ▶ print "Outputting side result to: ${file_name}\n" if $options->{debug_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"debug_mode"} 0 0 0
169 0 ▶ if ($file_name eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_name eq '-') { } 0 0 0
170 0 write_handle(\*STDOUT, $content, $missing_final_separator, $options);
171 } else {
172 write_file($file_name, $content, $missing_final_separator,
173 0 $known_side_output{$file_name}++, $options);
174 }
175 }
176  
177 # Returns (\@content, $missing_final_separator).
178 0 ▶sub read_side_input {
Subroutine read_side_input: not called CC=2 SCAR=17.9
Pod undocumented
179 0 my ($input, $options) = @_;
180 0 ▶ if ($input eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($input eq '-') { } 0 0 0
181 0 return read_stdin($options);
182 } else {
183 0 return read_file($input, $options);
184 }
185 }
186  
187 2 1;
« PTP/Commands.pm ↑ Summary PTP/Args.pm »