| 1 | package Indent; | ||||||||||
| 2 | |||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 7 | ▶ | use Class::Utils qw(set_params); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 7 | ▶ | use Error::Pure qw(err); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 7 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | # Constants. | ||||||||||
| 11 | 7 | Readonly::Scalar my $EMPTY_STR => q{}; | |||||||||
| 12 | |||||||||||
| 13 | 7 | our $VERSION = 0.08; | |||||||||
| 14 | |||||||||||
| 15 | # Constructor. | ||||||||||
| 16 | sub new { | ||||||||||
| 17 | 16 | ▶ | my ($class, @params) = @_; | ||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 18 | 16 | my $self = bless {}, $class; | |||||||||
| 19 | |||||||||||
| 20 | # Default indent. | ||||||||||
| 21 | 16 | $self->{'indent'} = $EMPTY_STR; | |||||||||
| 22 | |||||||||||
| 23 | # Every next indent string. | ||||||||||
| 24 | 16 | $self->{'next_indent'} = "\t"; | |||||||||
| 25 | |||||||||||
| 26 | # Process params. | ||||||||||
| 27 | 16 | set_params($self, @params); | |||||||||
| 28 | |||||||||||
| 29 | # Check to 'next_indent' parameter. | ||||||||||
| 30 | 14 | ▶ | if (! defined $self->{'next_indent'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 1 | err "'next_indent' parameter must be defined."; | |||||||||
| 32 | } | ||||||||||
| 33 | 13 | ▶ | if (ref $self->{'next_indent'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 2 | err "'next_indent' parameter must be a string."; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | # Check to 'indent' parameter. | ||||||||||
| 38 | 11 | ▶ | if (! defined $self->{'indent'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 1 | err "'indent' parameter must be defined."; | |||||||||
| 40 | } | ||||||||||
| 41 | 10 | ▶ | if (ref $self->{'indent'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 2 | err "'indent' parameter must be a string."; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | # Object. | ||||||||||
| 46 | 8 | return $self; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | # Add an indent to global indent. | ||||||||||
| 50 | sub add { | ||||||||||
| 51 | 7 | ▶ | my ($self, $indent) = @_; | ||||||||
|
Subroutine
add: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 52 | 7 | ▶ | if (! defined $indent) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 2 | $indent = $self->{'next_indent'}; | |||||||||
| 54 | } | ||||||||||
| 55 | 7 | $self->{'indent'} .= $indent; | |||||||||
| 56 | 7 | return 1; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | # Get a indent value. | ||||||||||
| 60 | sub get { | ||||||||||
| 61 | 13 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 62 | 13 | return $self->{'indent'}; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | # Remove an indent from global indent. | ||||||||||
| 66 | sub remove { | ||||||||||
| 67 | 3 | ▶ | my ($self, $indent) = @_; | ||||||||
|
Subroutine
remove: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 68 | 3 | ▶ | if (! defined $indent) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 1 | $indent = $self->{'next_indent'}; | |||||||||
| 70 | } | ||||||||||
| 71 | 3 | my $indent_length = length $indent; | |||||||||
| 72 | 3 | ▶ | if (substr($self->{'indent'}, -$indent_length) ne $indent) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 1 | err "Cannot remove indent '$indent'."; | |||||||||
| 74 | } | ||||||||||
| 75 | 2 | $self->{'indent'} = substr $self->{'indent'}, 0, -$indent_length; | |||||||||
| 76 | 2 | return 1; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | # Reseting indent. | ||||||||||
| 80 | sub reset { | ||||||||||
| 81 | 2 | ▶ | my ($self, $reset_value) = @_; | ||||||||
|
Subroutine
reset: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 82 | 2 | ▶ | if (! defined $reset_value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 1 | $reset_value = $EMPTY_STR; | |||||||||
| 84 | } | ||||||||||
| 85 | 2 | $self->{'indent'} = $reset_value; | |||||||||
| 86 | 2 | return 1; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 7 | 1; | |||||||||
| 90 | |||||||||||
| 91 | __END__ | ||||||||||