blib/lib/Filter/Indent/HereDoc.pm

filter: Statementstmt 72.5% 45 / 62 Branchbran 46.4% 13 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 62.7% 64 / 102 CC 19 SCAR 36.8
CC
19
Cov
62%
process_line
52.3
import
2.0
BEGIN
1.0
CRAP 39.5
↑ Summary
1 package Filter::Indent::HereDoc;
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 Filter::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '1.01';
8 1 our %options = ();
9 1 our @buffer; # Temporary storage of current here document
10 1 our @termstring; # FIFO list of here document terminating strings
11  
12 1 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
13 1 %options = ();
14 1 $options{$_}++ foreach (@_);
15 }
16  
17 FILTER_ONLY
18 1 ▶ executable => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
19 1 my @code = split /\n/;
20 1 $_ = join '',(map &process_line($_),@code);
21 1 };
22  
23 11 ▶sub process_line {
Subroutine process_line: called CC=18 SCAR=39.6
Pod undocumented
24 11 my $line = shift;
25 11 ▶ if (@termstring) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Filter::Indent::HereDoc::termstring) { } 2 9 11
26 # At this point we are in a here document, so all lines of code
27 # are buffered until the end of the heredoc is detected
28 2 push @buffer,$line;
29
30 # 2 scenarios - terminator is a blank line, or terminator contains non-
31 # whitespace. If blank line, then look for same whitespace at start of
32 # each line in buffer. Otherwise take the whitespace that precedes the
33 # terminator and match this against each line in the buffer.
34 #
35 # By default, we accept terminator strings in the Perl6 RFC111 format,
36 # i.e. whitespace, ';', and comments following the terminator are
37 # allowed. The only exception is if the terminator is a blank line,
38 # in this case then only whitespace is allowed.
39
40 2 my $termregex;
41 2 ▶ unless ($options{strict_terminators}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Filter::Indent::HereDoc::options{'strict_terminators'}) { } 0 2 2
42 2 ▶ if ($termstring[0] =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Filter::Indent::HereDoc::termstring[0] =~ /\S/) { } 2 0 2
43 2 $termregex = qr/^(\s*)($termstring[0])(\s*;{0,1}\s*(?:#.*){0,1})$/;
44 } else {
45 0 $termregex = qr/^\s*$/;
46 }
47 } else {
48 0 ▶ if ($termstring[0] =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Filter::Indent::HereDoc::termstring[0] =~ /\S/) { } 0 0 0
49 0 $termregex = qr/^(\s*)($termstring[0])$/;
50 } else {
51 0 $termregex = qr/^$/;
52 }
53 }
54
55 2 my ($whitespace,$terminator,$extras);
56 2 ▶ if ($line =~ $termregex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$termregex/) 1 1 2
57 1 ($whitespace,$terminator,$extras) = ($1,$2,$3);
58 1 ▶ if ($termstring[0] =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Filter::Indent::HereDoc::termstring[0] =~ /\S/) { } 1 0 1
59 1 foreach (@buffer) {
60 2 ▶ return unless (/^$whitespace/);
Branch: 1 branch
BranchTrueFalseTotal
unless /^$whitespace/ 0 2 2
61 }
62 } else {
63 # Terminator string is a blank line
64 0 undef $whitespace;
65 0 foreach (@buffer) {
66 0 ▶ if (/^(\s+)\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\s+)\S/) 0 0 0
67 0 ▶ $whitespace = $1 unless ($whitespace and /^$whitespace\s*/);
Branch: 1 branch
BranchTrueFalseTotal
unless $whitespace and /^$whitespace\s*/ 0 0 0
Condition0%0/3
$whitespace and /^$whitespace\s*/
!ll&&!rl&&r
000
MC/DC0%0/2
$whitespace and /^$whitespace\s*/
$whitespace /^$whitespace\s*/
Truth table0%0/3
A and B
A= $whitespaceB= /^$whitespace\s*/
ABresult
0X0
100
111
68 }
69 }
70 }
71 # End of heredoc - strip the required amount of whitespace
72 1 map s/^$whitespace//,@buffer;
73
74 # If we found extra characters after the terminator (Perl6 RFC111
75 # style), move them onto a new line to be compatible with Perl5
76 1 ▶ if ($extras) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extras) 0 1 1
77 0 pop @buffer;
78 0 push @buffer,$terminator;
79 0 push @buffer,$extras;
80 }
81
82 # Return captured heredoc back to Perl and reset the buffer
83 1 $line = join "\n",@buffer;
84 1 @buffer = ();
85 1 shift @termstring;
86 1 return "$line\n";
87 }
88 } else {
89 # Perl6 RFC111 states that whitespace after the terminator
90 # should be ingored
91 9 ▶ unless ($options{strict_terminators}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Filter::Indent::HereDoc::options{"strict_terminators"}) 9 0 9
92 9 $line =~ s/(?<!<)<<(?!<)\s+/<</g;
93 }
94  
95 # Can we find the start of any here documents?
96 9 ▶ MATCH: while ($line =~ m/(?<!<)<<(?!<)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /(?<!<)<<(?!<)/g) 1 9 10
97 1 ▶ if ($line =~ m/\G(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\G(\w+)/) 1 0 1
98 1 push @termstring,$1;
99 1 next MATCH;
100 }
101  
102 0 ▶ if ($line =~ m/\G(?:\s*(['"`]))(.*?)(?<!\\)\1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\G(?:\s*(['"`]))(.*?)(?<!\\)\1/) 0 0 0
103 0 my ($quote,$string) = ($1,$2);
104 0 $string =~ s/\\$quote/$quote/g;
105 0 push @termstring,$string;
106 0 next MATCH;
107 }
108
109 # Use of bare << to mean <<"" is depreciated
110 # ...but still works so the module needs to support it!
111 0 push @termstring,'';
112 }
113 9 return "$line\n";
114 }
115 }
116  
117 1 1;
118 __END__
↑ Summary