blib/lib/Text/ASCIIPipe.pm

filter: Statementstmt 13.0% 13 / 100 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 6.2% 1 / 16 Podpod 100% 9 / 9 totaltotal 11.3% 23 / 202 CC 40 SCAR 50.2
CC
40
Cov
7%
process
380.0
pull_file
90.0
fetch
20.0
CRAP 151.0
↑ Summary
1 package Text::ASCIIPipe;
2 # See POD below.
3  
4 # TODO: sanitize line end from handlers!
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 # major.minor.bugfix, the latter two with 3 digits each
8 # or major.minor_alpha
9 2 our $VERSION = '1.001000';
10 2 $VERSION = eval $VERSION;
11  
12  
13 # State codes. The numerocal values are important for the code in this module (used as array indices).
14 2 our $funky = 0;
15 2 our $line = 1;
16 2 our $begin = 2; # From here identical to ASCII code
17 2 our $end = 3;
18 2 our $allend = 4;
19  
20 # The actualy control characters.
21 2 my %control = (stx=>"\002", eot=>"\003", etx=>"\004");
22  
23 # Map plain character codes to state codes.
24 # I do not really care about codes 0 and 1.
25 2 my @codemap = ($funky, $funky, $begin, $end, $allend);
26  
27 sub fetch
28 0 ▶{
Subroutine fetch: not called CC=4 SCAR=30.0
Pod documented
29 0 my $fh = shift;
30 0 ▶ $fh = \*STDIN unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 0 0
31  
32 # Note to self: MAC style line endings will only work here for an old-style MAC Perl. All others see one big line with CRs in it.
33 0 $_[0] = <$fh>;
34 0 ▶ return undef unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
35 0 my $code = ord($_[0]);
36 0 ▶ return $line if $code > 4;
Branch: 1 branch
BranchTrueFalseTotal
if $code > 4 0 0 0
37 0 return $codemap[$code];
38 }
39  
40 # plain text means: first byte does not qualify as control code
41 sub plaintext
42 0 ▶{
Subroutine plaintext: not called CC=1 SCAR=6.9
Pod documented
43 0 my $code = ord($_[0]);
44 0 return ($code > 4);
45 }
46  
47 sub process
48 0 ▶{
Subroutine process: not called CC=19 SCAR=59.4
Pod documented
49 0 my %arg = @_;
50  
51 0 my $fline;
52 0 my $lend = undef;
53 0 my $in = $arg{in};
54 0 my $out = $arg{out};
55 0 ▶ $out = \*STDOUT unless defined $out;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 0 0 0
56 0 ▶ $arg{flush} = 1 unless defined $arg{flush};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg{"flush"} 0 0 0
57  
58 # The begin function is special because we ensure that the output gets flushed to prevent stalling of the pipe readers.
59 # Function table according to numerical value of state codes.
60 # A handler gets the current line as first in/output argument and the detected line end as second.
61 0 ▶ my $prefilter = defined $arg{pre} ? $arg{pre} : sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'pre'} ? : 0 0 0
62 my @handlers =
63 (
64 0 ▶ sub {} # 0 is some unknown crap. Ignore.
Subroutine __ANON__: not called CC=1 SCAR=0.0
65 0 ▶ ,defined $arg{line} ? $arg{line} : sub {}
Subroutine __ANON__: not called CC=1 SCAR=0.0
66 ,sub
67 0 ▶ {
Subroutine __ANON__: not called CC=3 SCAR=24.8
68 # That flush may be better placed after $arg{end} ...
69 0 ▶ if($arg{flush})
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"flush"}) 0 0 0
70 {
71 0 my $old = select($out);
72 0 $|=1;
73 0 select($old);
74 }
75 0 ▶ &{$arg{begin}}(@_) if defined $arg{begin};
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg{"begin"} 0 0 0
76 }
77 0 ▶ ,defined $arg{end} ? $arg{end} : sub {}
Subroutine __ANON__: not called CC=1 SCAR=0.0
78 0 ▶ ,defined $arg{allend} ? $arg{allend} : sub {}
Subroutine __ANON__: not called CC=1 SCAR=0.0
79 0 ▶ );
Branch: 3 branches
BranchTrueFalseTotal
defined $arg{'line'} ? : 0 0 0
defined $arg{'end'} ? : 0 0 0
defined $arg{'allend'} ? : 0 0 0
80  
81 0 my @gotcode; # Need to check if control codes were actually present.
82 my @prearg;
83 0 ▶ push(@prearg, $arg{handle}) if defined $arg{handle};
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg{"handle"} 0 0 0
84 0 ▶ while(defined (my $state = fetch($in, $fline)))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $state = fetch($in, $fline))) 0 0 0
85 {
86 # Store first encountered line end to be used for constructed lines.
87 0 ▶ unless(defined $lend)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lend) 0 0 0
88 {
89 0 $fline =~ m/([\012\015]*)$/;
90 0 $lend = $1;
91 }
92 0 $gotcode[$state] = 1;
93 0 ▶ if(not $gotcode[$begin])
Branch: 1 branch
BranchTrueFalseTotal
unless ($gotcode[$Text::ASCIIPipe::begin]) 0 0 0
94 {
95 0 my $extraline = '';
96 # Trigger begin hook implicitly when just encountering data.
97 0 &{$handlers[$begin]}(@prearg, $extraline, $lend);
98 0 print $out $extraline;
99 0 $gotcode[$begin] = 1;
100 }
101 0 ▶ if($state == $line)
Branch: 1 branch
BranchTrueFalseTotal
if ($state == $Text::ASCIIPipe::line) { } 0 0 0
102 {
103 0 ▶ next if &{$prefilter}(@prearg, $fline, $lend);
Branch: 1 branch
BranchTrueFalseTotal
if &{$prefilter;}(@prearg, $fline, $lend) 0 0 0
104 # A line handler shall modify the line.
105 0 &{$handlers[$state]}(@prearg, $fline, $lend);
106 0 ▶ print $out $fline if $fline ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $fline ne "" 0 0 0
107 }
108 else
109 {
110 # Other handlers could generate something.
111 0 my $extraline = '';
112 0 &{$handlers[$state]}(@prearg, $extraline, $lend);
113 0 ▶ print $out ($state == $begin ? $fline.$extraline : $extraline.$fline);
Branch: 1 branch
BranchTrueFalseTotal
$state == $Text::ASCIIPipe::begin ? : 0 0 0
114 }
115  
116 0 ▶ return if($state == $allend);
Branch: 1 branch
BranchTrueFalseTotal
if $state == $Text::ASCIIPipe::allend 0 0 0
117 # If we got proper end code, clear records for next file.
118 0 ▶ @gotcode = () if($state == $end);
Branch: 1 branch
BranchTrueFalseTotal
if $state == $Text::ASCIIPipe::end 0 0 0
119 }
120  
121 # Make sure that handlers get called even for empty file, or partial transfer with missing end markers.
122 # I wonder if $lend should be simply guessed to "\n".
123 0 for my $c ($begin,$end,$allend)
124 {
125 0 ▶ next if $gotcode[$c];
Branch: 1 branch
BranchTrueFalseTotal
if $gotcode[$c] 0 0 0
126 # The handlers are allowed to generate some output.
127 0 my $extraline = '';
128 0 &{$handlers[$c]}(@prearg, $extraline, $lend);
129 0 print $out $extraline;
130 }
131 }
132  
133 # Simple shortcut to pull a full file from pipe handle into an output file.
134 # Returns indication if this was not the last file (so you can loop until it returns a non-true value).
135 # return value:
136 # <0: Pull was invalid, no data for file (not even an empty one).
137 # 0: Pull successful, but this was it, no more files to expect.
138 # >0: Pull successful and no indication that there is not more to come.
139  
140 sub pull_file
141 0 ▶{
Subroutine pull_file: not called CC=9 SCAR=45.0
Pod documented
142 0 my ($from, $to) = @_;
143 0 ▶ $from = \*STDIN unless defined $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 0 0 0
144 0 ▶ $to = \*STDOUT unless defined $to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to 0 0 0
145 0 my $payload;
146 my $state;
147 0 ▶ while(defined ($state = fetch($from, $payload)))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($state = fetch($from, $payload))) 0 0 0
148 {
149 0 ▶ next if $state == $begin;
Branch: 1 branch
BranchTrueFalseTotal
if $state == $Text::ASCIIPipe::begin 0 0 0
150 0 ▶ last if $state != $line;
Branch: 1 branch
BranchTrueFalseTotal
if $state != $Text::ASCIIPipe::line 0 0 0
151 0 print $to $payload;
152 }
153  
154 # All fine: Loop ended with orderly file end marker.
155 0 ▶ return 1 if(defined $state and $state != $allend);
Branch: 1 branch
BranchTrueFalseTotal
if defined $state and $state != $Text::ASCIIPipe::allend 0 0 0
Condition0%0/3
defined $state and $state != $Text::ASCIIPipe::allend
!ll&&!rl&&r
000
MC/DC0%0/2
defined $state and $state != $Text::ASCIIPipe::allend
defined $state $state != $Text::ASCIIPipe::allend
Truth table0%0/3
A and B
A= defined $stateB= $state != $Text::ASCIIPipe::allend
ABresult
0X0
100
111
156 # Ended on EOF (or some esoteric error we still treat as such),
157 # as there was no allend or error before that, just assume normal end of things.
158 0 ▶ return 0 if(not defined $state);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state 0 0 0
159 # If we hit allend, we did not stop with an orderly file end,
160 # so must assume we got nothing at all.
161 0 return -1; # if($state == allend) is already implied
162 }
163  
164 # The senders.
165  
166 sub push_file
167 0 ▶{
Subroutine push_file: not called CC=4 SCAR=30.0
Pod documented
168 0 my ($from, $to) = @_;
169 0 ▶ $from = \*STDIN unless defined $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 0 0 0
170 0 ▶ $to = \*STDOUT unless defined $to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to 0 0 0
171  
172 0 file_begin($to);
173 0 ▶ while(<$from>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $from)) 0 0 0
174 {
175 0 print $to $_;
176 }
177 0 file_end($to);
178 0 return 1; # Should it return something special?
179 }
180  
181 sub file_begin
182 0 ▶{
Subroutine file_begin: not called CC=2 SCAR=17.9
Pod documented
183 0 my $to = shift;
184 0 ▶ $to = \*STDOUT unless defined $to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to 0 0 0
185  
186 0 print $to $control{stx}."\n";
187 }
188  
189 sub file_lines
190 0 ▶{
Subroutine file_lines: not called CC=3 SCAR=24.8
Pod documented
191 0 my $to = shift;
192 0 ▶ $to = \*STDOUT unless defined $to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to 0 0 0
193  
194 0 for(@_){ print $to $_; }
195 }
196  
197 sub file_end
198 0 ▶{
Subroutine file_end: not called CC=2 SCAR=17.9
Pod documented
199 0 my $to = shift;
200 0 ▶ $to = \*STDOUT unless defined $to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to 0 0 0
201  
202 0 print $to $control{eot}."\n";
203 }
204  
205 sub done
206 0 ▶{
Subroutine done: not called CC=2 SCAR=17.9
Pod documented
207 0 my $to = shift;
208 0 ▶ $to = \*STDOUT unless defined $to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to 0 0 0
209  
210 0 print $to $control{etx}."\n";
211 }
212  
213 2 1;
214  
215 __END__
↑ Summary