line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Config::Neat::Array - Class for array-like config nodes |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 COPYRIGHT |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Copyright (C) 2012-2015 Igor Afanasyev |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SEE ALSO |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
L |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=cut |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
package Config::Neat::Array; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = '1.4'; |
18
|
|
|
|
|
|
|
|
19
|
4
|
|
|
4
|
|
22
|
use strict; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
108
|
|
20
|
|
|
|
|
|
|
|
21
|
4
|
|
|
4
|
|
17
|
no warnings qw(uninitialized); |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
138
|
|
22
|
|
|
|
|
|
|
|
23
|
4
|
|
|
4
|
|
711
|
use Config::Neat::Util qw(is_any_array is_neat_array); |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
2440
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub new { |
26
|
789
|
|
|
789
|
0
|
1295
|
my ($class, $self) = @_; |
27
|
789
|
100
|
66
|
|
|
1674
|
$self = [] unless defined $self && ref($self) eq 'ARRAY'; |
28
|
789
|
|
|
|
|
988
|
bless $self, $class; |
29
|
789
|
|
|
|
|
1931
|
return $self; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub push { |
33
|
18
|
|
|
18
|
0
|
103
|
my $self = shift; |
34
|
18
|
|
|
|
|
46
|
push @$self, @_; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# return a flattened one-dimensional array, where nested |
38
|
|
|
|
|
|
|
# Config::Neat arrays are expanded recursively |
39
|
|
|
|
|
|
|
sub as_flat_array { |
40
|
215
|
|
|
215
|
0
|
289
|
my ($self) = @_; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# fist check if conversion will be needed |
43
|
215
|
|
|
|
|
223
|
my $need_conversion; |
44
|
215
|
|
|
|
|
301
|
foreach my $val (@$self) { |
45
|
283
|
100
|
|
|
|
409
|
if (is_neat_array($val)) { |
46
|
10
|
|
|
|
|
13
|
$need_conversion = 1; |
47
|
10
|
|
|
|
|
15
|
last; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
215
|
100
|
|
|
|
490
|
return $self unless $need_conversion; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# flatten the array recursively |
53
|
10
|
|
|
|
|
19
|
my $result = Config::Neat::Array->new(); |
54
|
10
|
|
|
|
|
18
|
foreach my $val (@$self) { |
55
|
28
|
50
|
|
|
|
45
|
if (is_neat_array($val)) { |
56
|
28
|
|
|
|
|
45
|
$val = $val->as_flat_array; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
28
|
50
|
|
|
|
46
|
if (is_any_array($val)) { |
60
|
|
|
|
|
|
|
# expand arrays |
61
|
28
|
|
|
|
|
60
|
push @$result, @$val; |
62
|
|
|
|
|
|
|
} else { |
63
|
|
|
|
|
|
|
#push scalars and hashes as is |
64
|
0
|
|
|
|
|
0
|
push @$result, $val; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
10
|
|
|
|
|
25
|
return $result; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Given ['foo', 'bar', 'baz'] as the contents of the array, returns 'foo bar baz' string. |
71
|
|
|
|
|
|
|
# Array is flattened before being converted into a string. |
72
|
|
|
|
|
|
|
# If string starts from a newline and the next line is indented, remove that amount of spaces |
73
|
|
|
|
|
|
|
# from each line and trim leading and trailing newline |
74
|
|
|
|
|
|
|
sub as_string { |
75
|
159
|
|
|
159
|
0
|
4114
|
my ($self) = @_; |
76
|
|
|
|
|
|
|
|
77
|
159
|
|
|
|
|
182
|
my $val = join(' ', @{$self->as_flat_array}); |
|
159
|
|
|
|
|
227
|
|
78
|
159
|
|
|
|
|
205
|
my $indent = undef; |
79
|
159
|
|
|
|
|
341
|
while ($val =~ m/\n(\s+)/g) { |
80
|
0
|
|
|
|
|
0
|
my $len = length($1); |
81
|
0
|
0
|
0
|
|
|
0
|
$indent = $len unless defined $indent and $len > 0; |
82
|
0
|
0
|
0
|
|
|
0
|
$indent = $len if $len > 0 and $indent > $len; |
83
|
|
|
|
|
|
|
} |
84
|
159
|
50
|
|
|
|
283
|
if ($indent > 0) { |
85
|
0
|
|
|
|
|
0
|
$indent = ' ' x $indent; |
86
|
0
|
|
|
|
|
0
|
$val =~ s/\n$indent/\n/sg; |
87
|
0
|
|
|
|
|
0
|
$val =~ s/^\s*\n//s; # remove first single newline and preceeding whitespace |
88
|
0
|
|
|
|
|
0
|
$val =~ s/\n\s*$//s; # remove last single newline and whitespace after it |
89
|
|
|
|
|
|
|
} |
90
|
159
|
|
|
|
|
466
|
return $val; |
91
|
|
|
|
|
|
|
} # end sub |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Returns true if the string representation of the array |
94
|
|
|
|
|
|
|
# evaluates case-insensitively to a known list of positive boolean strings |
95
|
|
|
|
|
|
|
sub as_boolean { |
96
|
21
|
|
|
21
|
0
|
88
|
my ($self) = @_; |
97
|
|
|
|
|
|
|
|
98
|
21
|
|
|
|
|
53
|
return ($self->as_string =~ m/^(YES|Y|ON|TRUE|1)$/i); |
99
|
|
|
|
|
|
|
} # end sub |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# Returns true if the string representation of the array |
102
|
|
|
|
|
|
|
# evaluates case-insensitively to a known list of positive or negative boolean strings |
103
|
|
|
|
|
|
|
sub is_boolean { |
104
|
11
|
|
|
11
|
0
|
15
|
my ($self) = @_; |
105
|
|
|
|
|
|
|
|
106
|
11
|
|
|
|
|
16
|
return ($self->as_string =~ m/^(YES|NO|Y|N|ON|OFF|TRUE|FALSE|1|0)$/i); |
107
|
|
|
|
|
|
|
} # end sub |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Given ['foo', 'bar', 'baz'] as the contents of the array, |
110
|
|
|
|
|
|
|
# and property name 'x', returns the following hash reference: |
111
|
|
|
|
|
|
|
# { |
112
|
|
|
|
|
|
|
# 0 => {'x' => 'foo'}, |
113
|
|
|
|
|
|
|
# 1 => {'x' => 'bar'}, |
114
|
|
|
|
|
|
|
# 2 => {'x' => 'baz'} |
115
|
|
|
|
|
|
|
# } |
116
|
|
|
|
|
|
|
sub as_hash { |
117
|
0
|
|
|
0
|
0
|
|
my ($self, $propname) = @_; |
118
|
|
|
|
|
|
|
|
119
|
0
|
0
|
|
|
|
|
die "Second parameter (propname) not provided" unless defined $propname; |
120
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
my $result = {}; |
122
|
0
|
|
|
|
|
|
tie(%$result, 'Tie::IxHash'); |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
my $n = 0; |
125
|
0
|
|
|
|
|
|
foreach my $val (@$self) { |
126
|
0
|
|
|
|
|
|
$result->{$n++} = {$propname => $val}; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
return $result; |
130
|
|
|
|
|
|
|
} # end sub |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
1; |