File Coverage

blib/lib/Acme/Indent.pm
Criterion Covered Total %
statement 33 33 100.0
branch 12 12 100.0
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 49 50 98.0


line stmt bran cond sub pod time code
1             package Acme::Indent;
2              
3 1     1   814 use strict;
  1         2  
  1         40  
4 1     1   6 use warnings;
  1         2  
  1         35  
5              
6 1     1   15 use Carp;
  1         3  
  1         471  
7              
8             require Exporter;
9             our @ISA = qw(Exporter);
10             our @EXPORT = qw(ai);
11             our @EXPORT_OK = qw();
12             our $VERSION = '0.02';
13              
14             sub ai {
15 2     2 0 1765 my @lines = split m{\n}xms, $_[0];
16              
17 2         4 my $result = '';
18 2         2 my $empty = '';
19              
20 2         3 my $shft = 0;
21 2         2 my $data = 0;
22              
23 2         3 for my $l (@lines) {
24 20 100       36 unless ($data) {
25 4 100       9 if ($l =~ m{\A (\s*) \S}xms) {
26 2         4 $shft = length($1);
27 2         3 $data = 1;
28             }
29             }
30              
31 20 100       26 if ($data) {
32 18         15 my ($spaces, $text);
33              
34 18 100       23 if (length($l) >= $shft) {
35 16         17 $spaces = substr($l, 0, $shft);
36 16         23 $text = substr($l, $shft);
37             }
38             else {
39 2         2 $spaces = $l;
40 2         3 $text = '';
41             }
42              
43 18 100       32 if ($spaces =~ m{\S}xms) {
44 1         182 carp "Found characters ('$spaces') in indentation zone";
45             }
46              
47 18 100       36 if ($text =~ m{\S}xms) {
48 15         39 $result .= $empty.$text."\n";
49 15         23 $empty = '';
50             }
51             else {
52 3         4 $empty .= "\n";
53             }
54             }
55             }
56              
57 2         7 return $result;
58             }
59              
60             1;
61              
62             __END__