File Coverage

blib/lib/String/Nudge.pm
Criterion Covered Total %
statement 26 26 100.0
branch 4 4 100.0
condition 3 3 100.0
subroutine 5 5 100.0
pod 1 1 100.0
total 39 39 100.0


line stmt bran cond sub pod time code
1 1     1   76784 use 5.10.0;
  1         14  
2 1     1   6 use strict;
  1         11  
  1         29  
3 1     1   14 use warnings;
  1         2  
  1         71  
4              
5             package String::Nudge;
6              
7             # ABSTRACT: Indents all lines in a multi-line string
8             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9             our $VERSION = '1.0001';
10              
11 1     1   7 use Exporter 'import';
  1         2  
  1         227  
12             our @EXPORT = qw/nudge/;
13              
14             sub nudge ($;$) {
15 11     11 1 726 my $first = shift;
16 11         18 my $second = shift;
17              
18 11         18 my $indent = 4;
19 11         13 my $string;
20              
21 11 100       25 if(defined $second) {
22 8 100 100     49 if(int $first eq $first && int $first >= 0) {
23 6         9 $indent = int $first;
24 6         10 $string = $second;
25             }
26             else {
27 2         381 warnings::warn(numeric => q{first argument to nudge not an integer >= 0.});
28 2         13 $string = $second;
29             }
30             }
31             else {
32 3         6 $string = $first;
33              
34             }
35 11         25 my $nudgement = ' ' x $indent;
36              
37 11         59 $string =~ s{^(?=\V)}{$nudgement}gms;
38 11         45 $string =~ s{^\h*$}{}gms;
39 11         49 return $string;
40             }
41              
42              
43             1;
44              
45             __END__
46              
47             =pod
48              
49             =encoding utf-8
50              
51             =head1 NAME
52              
53             String::Nudge - Indents all lines in a multi-line string
54              
55              
56              
57             =begin html
58              
59             <p>
60             <img src="https://img.shields.io/badge/perl-5.10+-blue.svg" alt="Requires Perl 5.10+" />
61             <a href="https://travis-ci.org/Csson/p5-String-Nudge"><img src="https://api.travis-ci.org/Csson/p5-String-Nudge.svg?branch=master" alt="Travis status" /></a>
62             <a href="http://cpants.cpanauthors.org/dist/String-Nudge-1.0001"><img src="https://badgedepot.code301.com/badge/kwalitee/String-Nudge/1.0001" alt="Distribution kwalitee" /></a>
63             <a href="http://matrix.cpantesters.org/?dist=String-Nudge%201.0001"><img src="https://badgedepot.code301.com/badge/cpantesters/String-Nudge/1.0001" alt="CPAN Testers result" /></a>
64             <img src="https://img.shields.io/badge/coverage-100.0%-brightgreen.svg" alt="coverage 100.0%" />
65             </p>
66              
67             =end html
68              
69             =head1 VERSION
70              
71             Version 1.0001, released 2016-02-19.
72              
73             =head1 SYNOPSIS
74              
75             use String::Nudge;
76              
77             sub out {
78             print nudge q{
79             A long
80             text.
81             };
82             }
83              
84             # is exactly the same as
85             sub out {
86             print q{
87             A long
88             text.
89             };
90             }
91              
92             =head1 DESCRIPTION
93              
94             String::Nudge provides C<nudge>, a simple function that indents all lines in a multi line string.
95              
96             =head2 METHODS
97              
98             =head3 nudge $string
99              
100             # ' hello'
101             my $string = nudge 'hello';
102              
103             =head3 nudge $number_of_spaces, $string
104              
105             # ' hello'
106             my $string = nudge 8, 'hello';
107              
108             If C<$number_of_spaces> is not given (or isn't an integer >= 0) its default value is C<4>.
109              
110             Every line in C<$string> is indented by C<$number_of_spaces>. Lines only consisting of white space is trimmed (but not removed).
111              
112             =head2 MORE EXAMPLES
113              
114             =head3 Usage with L<qi|Syntax::Feature::Qi>
115              
116             L<Syntax::Feature::Qi> adds C<qi> and C<qqi> that removes the same amount of leading whitespace as the first (significant) line has from all lines in a string:
117              
118             # these three packages are equivalent:
119             package Example::Nudge {
120              
121             use String::Nudge;
122             use syntax 'qi';
123              
124             sub out {
125             print nudge qi{
126             sub funcname {
127             print 'stuff';
128             }
129             };
130             }
131             }
132             package Example::Q {
133              
134             sub out {
135             print q{
136             sub funcname {
137             print 'stuff';
138             }
139             };
140             }
141             }
142             package Example::HereDoc {
143              
144             sub out {
145              
146             (my $text = <<" END") =~ s{^ {8}}{}gm;
147             sub funcname {
148             print 'stuff';
149             }
150             END
151              
152             print $text;
153             }
154             }
155              
156             =head3 Usage with L<qs|Syntax::Feature::Qs>
157              
158             L<Syntax::Feature::Qs> adds C<qs> and C<qqs> that removes all leading whitespace from all lines in a string:
159              
160             # these three packages are equivalent:
161             package Example::Nudge {
162              
163             use String::Nudge;
164             use syntax 'qs';
165              
166             sub out {
167             print nudge qs{
168             This is
169             a multi line
170              
171             string.
172             };
173             }
174             }
175             package Example::Q {
176              
177             sub out {
178             print q{
179             This is
180             a multi line
181              
182             string.
183             };
184             }
185             }
186             package Example::HereDoc {
187              
188             sub out {
189              
190             (my $text = <<" END") =~ s{^ {8}}{}gm;
191             This is
192             a multi line
193              
194             string.
195             END
196              
197             print $text;
198             }
199             }
200              
201             =head1 SEE ALSO
202              
203             =over 4
204              
205             =item *
206              
207             L<Indent::String>
208              
209             =item *
210              
211             L<String::Indent>
212              
213             =item *
214              
215             L<qi|Syntax::Feature::Qi>
216              
217             =item *
218              
219             L<qs|Syntax::Feature::Qs>
220              
221             =back
222              
223             =head1 SOURCE
224              
225             L<https://github.com/Csson/p5-String-Nudge>
226              
227             =head1 HOMEPAGE
228              
229             L<https://metacpan.org/release/String-Nudge>
230              
231             =head1 AUTHOR
232              
233             Erik Carlsson <info@code301.com>
234              
235             =head1 COPYRIGHT AND LICENSE
236              
237             This software is copyright (c) 2016 by Erik Carlsson.
238              
239             This is free software; you can redistribute it and/or modify it under
240             the same terms as the Perl 5 programming language system itself.
241              
242             =cut