File Coverage

blib/lib/Indent.pm
Criterion Covered Total %
statement 49 49 100.0
branch 16 16 100.0
condition n/a
subroutine 10 10 100.0
pod 5 5 100.0
total 80 80 100.0


line stmt bran cond sub pod time code
1             package Indent;
2              
3 7     7   195019 use strict;
  7         15  
  7         242  
4 7     7   30 use warnings;
  7         19  
  7         453  
5              
6 7     7   3636 use Class::Utils qw(set_params);
  7         94206  
  7         147  
7 7     7   551 use Error::Pure qw(err);
  7         44  
  7         295  
8 7     7   34 use Readonly;
  7         12  
  7         3428  
9              
10             # Constants.
11             Readonly::Scalar my $EMPTY_STR => q{};
12              
13             our $VERSION = 0.09;
14              
15             # Constructor.
16             sub new {
17 16     16 1 1395705 my ($class, @params) = @_;
18 16         48 my $self = bless {}, $class;
19              
20             # Default indent.
21 16         70 $self->{'indent'} = $EMPTY_STR;
22              
23             # Every next indent string.
24 16         53 $self->{'next_indent'} = "\t";
25              
26             # Process params.
27 16         110 set_params($self, @params);
28              
29             # Check to 'next_indent' parameter.
30 14 100       197 if (! defined $self->{'next_indent'}) {
31 1         5 err "'next_indent' parameter must be defined.";
32             }
33 13 100       40 if (ref $self->{'next_indent'}) {
34 2         8 err "'next_indent' parameter must be a string.";
35             }
36              
37             # Check to 'indent' parameter.
38 11 100       32 if (! defined $self->{'indent'}) {
39 1         5 err "'indent' parameter must be defined.";
40             }
41 10 100       30 if (ref $self->{'indent'}) {
42 2         37 err "'indent' parameter must be a string.";
43             }
44              
45             # Object.
46 8         35 return $self;
47             }
48              
49             # Add an indent to global indent.
50             sub add {
51 7     7 1 26 my ($self, $indent) = @_;
52 7 100       22 if (! defined $indent) {
53 2         10 $indent = $self->{'next_indent'};
54             }
55 7         18 $self->{'indent'} .= $indent;
56 7         11 return 1;
57             }
58              
59             # Get a indent value.
60             sub get {
61 13     13 1 1078 my $self = shift;
62 13         64 return $self->{'indent'};
63             }
64              
65             # Remove an indent from global indent.
66             sub remove {
67 3     3 1 15 my ($self, $indent) = @_;
68 3 100       6 if (! defined $indent) {
69 1         2 $indent = $self->{'next_indent'};
70             }
71 3         5 my $indent_length = length $indent;
72 3 100       11 if (substr($self->{'indent'}, -$indent_length) ne $indent) {
73 1         8 err "Cannot remove indent '$indent'.";
74             }
75 2         5 $self->{'indent'} = substr $self->{'indent'}, 0, -$indent_length;
76 2         3 return 1;
77             }
78              
79             # Reseting indent.
80             sub reset {
81 2     2 1 7 my ($self, $reset_value) = @_;
82 2 100       9 if (! defined $reset_value) {
83 1         3 $reset_value = $EMPTY_STR;
84             }
85 2         7 $self->{'indent'} = $reset_value;
86 2         5 return 1;
87             }
88              
89             1;
90              
91             __END__
92              
93             =pod
94              
95             =encoding utf8
96              
97             =head1 NAME
98              
99             Indent - Class for indent handling.
100              
101             =head1 SYNOPSIS
102              
103             use Indent;
104              
105             my $indent = Indent->new(%parameters);
106             $indent->add([$cur_indent]);
107             my $string = $indent->get;
108             $indent->remove([$cur_indent]);
109             $indent->reset([$reset_value]);
110              
111             =head1 METHODS
112              
113             =head2 C<new>
114              
115             my $indent = Indent->new(%parameters);
116              
117             Constructor.
118              
119             Returns instance of object.
120              
121             =over 8
122              
123             =item * C<indent>
124              
125             Default indent.
126             Default value is ''.
127              
128             =item * C<next_indent>
129              
130             Next indent. Adding to internal indent variable after every add method
131             calling.
132             Default value is "\t" (tabelator).
133              
134             =back
135              
136             =head2 C<add>
137              
138             $indent->add([$cur_indent]);
139              
140             Method for adding C<$cur_indent>, if defined, or 'next_indent'.
141              
142             Returns undef.
143              
144             =head2 C<get>
145              
146             my $string = $indent->get;
147              
148             Get actual indent string.
149              
150             Returns string.
151              
152             =head2 C<remove>
153              
154             $indent->remove([$cur_indent]);
155              
156             Method for removing C<$cur_indent>, if defined, or 'next_indent'. Only if
157             is removable.
158              
159             Returns undef.
160              
161             =head2 C<reset>
162              
163             $indent->reset([$reset_value]);
164              
165             Resets internal indent string to C<$reset_value> or ''.
166              
167             Returns undef.
168              
169             =head1 ERRORS
170              
171             new():
172             'next_indent' parameter must be defined.
173             'next_indent' parameter must be a string.
174             'indent' parameter must be defined.
175             'indent' parameter must be a string.
176             From Class::Utils::set_params():
177             Unknown parameter '%s'.
178              
179             remove():
180             Cannot remove indent '$indent'.
181              
182             =head1 EXAMPLE
183              
184             use strict;
185             use warnings;
186              
187             use Indent;
188              
189             # Indent object.
190             my $indent = Indent->new(
191              
192             # Begin indent.
193             'indent' => '->',
194              
195             # Next indent.
196             'next_indent' => "->"
197             );
198              
199             # Print example.
200             print $indent->get;
201             print "Example\n";
202              
203             # Add indent and print ok.
204             $indent->add;
205             print $indent->get;
206             print "Ok\n";
207              
208             # Remove indent and print nex example.
209             $indent->remove;
210             print $indent->get;
211             print "Example2\n";
212              
213             # Reset.
214             $indent->reset;
215              
216             # Output:
217             # ->Example
218             # ->->Ok
219             # ->Example2
220              
221             =head1 DEPENDENCIES
222              
223             L<Class::Utils>,
224             L<Error::Pure>,
225             L<Readonly>.
226              
227             =head1 SEE ALSO
228              
229             =over
230              
231             =item L<Indent::Block>
232              
233             Class for block indenting.
234              
235             =item L<Indent::Data>
236              
237             Class for data indenting.
238              
239             =item L<Indent::String>
240              
241             Class for text indenting.
242              
243             =item L<Indent::Utils>
244              
245             Utilities for Indent classes.
246              
247             =item L<Indent::Word>
248              
249             Class for word indenting.
250              
251             =back
252              
253             =head1 REPOSITORY
254              
255             L<https://github.com/michal-josef-spacek/Indent>
256              
257             =head1 AUTHOR
258              
259             Michal Josef Špaček L<mailto:skim@cpan.org>
260              
261             L<http://skim.cz>
262              
263             =head1 LICENSE AND COPYRIGHT
264              
265             © 2005-2024 Michal Josef Špaček
266              
267             BSD 2-Clause License
268              
269             =head1 VERSION
270              
271             0.09
272              
273             =cut