File Coverage

blib/lib/Text/Continuation/Parser.pm
Criterion Covered Total %
statement 44 53 83.0
branch 26 38 68.4
condition 15 23 65.2
subroutine 7 7 100.0
pod 1 1 100.0
total 93 122 76.2


line stmt bran cond sub pod time code
1 1     1   76013 use utf8;
  1         22  
  1         16  
2 1     1   40 use 5.010;
  1         3  
3             package Text::Continuation::Parser;
4             our $VERSION = '0.5';
5 1     1   5 use warnings;
  1         2  
  1         23  
6 1     1   4 use strict;
  1         2  
  1         27  
7 1     1   5 use Carp qw(croak);
  1         1  
  1         46  
8              
9             # ABSTRACT: Parse files with continuation lines
10              
11 1     1   5 use base qw(Exporter);
  1         2  
  1         591  
12              
13             our @EXPORT_OK = qw(parse_line);
14              
15             my $trim = qr/(?:^\s*|\s*$)/;
16              
17             sub parse_line {
18 19     19 1 14083 my ($fh, $line) = @_;
19              
20 19         105 my $new = $fh->getline;
21              
22 19 100 100     358 if ($line && !defined $new) {
23 1         9 croak
24             "Line continuation detected and reaching end of file. This is invalid";
25             }
26 18 100       33 return unless defined $new;
27              
28 17         23 chomp($new);
29 17         31 $new =~ s/\r//g;
30 17 100       83 if ($new =~ /^\s*#/) {
    100          
    100          
    100          
    50          
31 1 50 50     5 if (length($line//'')) {
32 1         3 return parse_line($fh, $line);
33             }
34 0         0 return $new;
35             }
36             elsif ($new =~ /^\\$/) {
37 1         12 return parse_line($fh, $line);
38             }
39             elsif ($new =~ /\s+\\\s*$/) {
40 3         15 $new =~ s#\s+\\\s*$##;
41 3         22 $new =~ s/$trim//g;
42 3 100 100     13 if (length($line//'')) {
43 2 50       5 if (length($new)) {
44 2         18 return parse_line($fh, join(' ', $line, $new));
45             }
46 0         0 return parse_line($fh, $line);
47             }
48 1 50       6 return parse_line($fh, length($new) ? $new : undef);
49             }
50             elsif ($new =~ /\S+\\\s*$/) {
51 5         18 $new =~ s#\\\s*$##;
52 5         27 $new =~ s/$trim//g;
53 5 100 100     23 if (length($line//'')) {
54 2 50       4 if (length($new)) {
55 2         8 return parse_line($fh, join('', $line, $new));
56             }
57 0         0 return parse_line($fh, $line);
58             }
59 3 50       12 return parse_line($fh, length($new) ? $new : undef);
60             }
61             elsif ($new =~ /^\s+/) {
62 0         0 $new =~ s/$trim//g;
63 0 0 0     0 if (!length($new) && length($line//'')) {
      0        
64 0         0 croak
65             "Line continuation detected and empty line. This is invalid";
66             }
67 0 0 0     0 if (length($line//'')) {
68 0 0       0 return length($new) ? join(' ', $line, $new) : $line;
69             }
70 0         0 return $new;
71             }
72             else {
73 7         46 $new =~ s/$trim//g;
74 7 100 100     27 if (!length($new) && length($line//'')) {
      100        
75 1         14 croak
76             "Line continuation detected and empty line. This is invalid";
77             }
78 6 100 100     35 return length($line//'') ? join('', $line, $new) : $new;
79             }
80             }
81              
82             1;
83              
84             __END__