File Coverage

blib/lib/Filter/Undent.pm
Criterion Covered Total %
statement 24 25 96.0
branch 1 2 50.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 33 35 94.2


line stmt bran cond sub pod time code
1             package Filter::Undent;
2              
3 1     1   26096 use 5.006;
  1         3  
  1         32  
4              
5 1     1   4 use Exporter;
  1         2  
  1         59  
6             push @ISA, 'Exporter';
7             @EXPORT = qw(undent);
8              
9 1     1   5 use strict;
  1         4  
  1         34  
10 1     1   9 use warnings;
  1         2  
  1         27  
11 1     1   827 use Filter::Simple;
  1         53611  
  1         6  
12              
13             =head1 NAME
14              
15             Filter::Undent - Un-indent heredoc strings automatically
16              
17             =cut
18              
19             our $VERSION = '1.0.3';
20              
21             =head1 SYNOPSIS
22              
23             Don't you wish heredocs could align with your docs? Now they can!
24              
25             use Filter::Undent;
26             print <<'EOF'
27             What is printed is magically undented to the level of the
28             first line of the heredoc.
29              
30             Only these lines will be indented in the output, since they
31             are indented relative to the first line
32             EOF
33              
34             If you want to disable the unindent of the heredocs, simply:
35              
36             no Filter::Undent;
37              
38             =cut
39              
40             FILTER_ONLY
41             quotelike => sub {s{^<<}{undent <<}gs},
42             all => sub {
43             return unless $Filter::Undent::DEBUG;
44             print STDERR join '', map {"Filter::Undent> $_\n"} split /\n/, $_;
45             },
46             ;
47              
48             =head1 EXPORTED FUNCTIONS
49              
50             =head2 undent
51              
52             This function does the actual work of unindenting. It returns the modified
53             version of the input string, ignoring any leading newlines, and if the first
54             line of the provided string is indented with space or tab characters, it will
55             remove the same whitespace from the beginning of all of the subsequent lines
56             in the output. Any lines which are outdented from the first line, or is using
57             a different combination of spaces or tabs will not have its leading space
58             removed.
59              
60             =cut
61              
62             sub undent ($) {
63 1     1   157 no warnings 'uninitialized';
  1         2  
  1         149  
64 2 50   2 1 1251 if ( $_[0] =~ m/^(\r?\n)*([ \t]+)/ ) {
65 2         8 my $i = $2;
66 2         22 return join '', map { s/^\Q$i\E/$1/g; $_ } grep { $_ ne '' }
  7         62  
  7         27  
  13         28  
67             split /(.*?\n)/, $_[0];
68             }
69 0           return $_[0];
70             }
71              
72             =head1 AUTHOR
73              
74             Anthony Kilna, C<< >> - L
75              
76             =head1 BUGS
77              
78             Please report any bugs or feature requests to C, or through
79             the web interface at L. I will be notified, and then you'll
80             automatically be notified of progress on your bug as I make changes.
81              
82             =head1 SUPPORT
83              
84             You can find documentation for this module with the perldoc command.
85              
86             perldoc Filter::Undent
87              
88              
89             You can also look for information at:
90              
91             =over 4
92              
93             =item * RT: CPAN's request tracker (report bugs here)
94              
95             L
96              
97             =item * AnnoCPAN: Annotated CPAN documentation
98              
99             L
100              
101             =item * CPAN Ratings
102              
103             L
104              
105             =item * Search CPAN
106              
107             L
108              
109             =back
110              
111             =head1 LICENSE AND COPYRIGHT
112              
113             Copyright 2012 Kilna Companies.
114              
115             This program is free software; you can redistribute it and/or modify it
116             under the terms of either: the GNU General Public License as published
117             by the Free Software Foundation; or the Artistic License.
118              
119             See http://dev.perl.org/licenses/ for more information.
120              
121             =cut
122              
123             1; # End of Filter::Undent