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.302'; |
18
|
|
|
|
|
|
|
|
19
|
4
|
|
|
4
|
|
23
|
use strict; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
108
|
|
20
|
|
|
|
|
|
|
|
21
|
4
|
|
|
4
|
|
21
|
no warnings qw(uninitialized); |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
140
|
|
22
|
|
|
|
|
|
|
|
23
|
4
|
|
|
4
|
|
589
|
use Config::Neat::Util qw(is_any_array is_neat_array); |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
2274
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub new { |
26
|
788
|
|
|
788
|
0
|
1608
|
my ($class, $self) = @_; |
27
|
788
|
100
|
66
|
|
|
2295
|
$self = [] unless defined $self && ref($self) eq 'ARRAY'; |
28
|
788
|
|
|
|
|
1367
|
bless $self, $class; |
29
|
788
|
|
|
|
|
2367
|
return $self; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub push { |
33
|
18
|
|
|
18
|
0
|
140
|
my $self = shift; |
34
|
18
|
|
|
|
|
55
|
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
|
420
|
my ($self) = @_; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# fist check if conversion will be needed |
43
|
215
|
|
|
|
|
322
|
my $need_conversion; |
44
|
215
|
|
|
|
|
397
|
foreach my $val (@$self) { |
45
|
283
|
100
|
|
|
|
630
|
if (is_neat_array($val)) { |
46
|
10
|
|
|
|
|
17
|
$need_conversion = 1; |
47
|
10
|
|
|
|
|
22
|
last; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
215
|
100
|
|
|
|
717
|
return $self unless $need_conversion; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# flatten the array recursively |
53
|
10
|
|
|
|
|
28
|
my $result = Config::Neat::Array->new(); |
54
|
10
|
|
|
|
|
22
|
foreach my $val (@$self) { |
55
|
28
|
50
|
|
|
|
68
|
if (is_neat_array($val)) { |
56
|
28
|
|
|
|
|
63
|
$val = $val->as_flat_array; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
28
|
50
|
|
|
|
72
|
if (is_any_array($val)) { |
60
|
|
|
|
|
|
|
# expand arrays |
61
|
28
|
|
|
|
|
85
|
push @$result, @$val; |
62
|
|
|
|
|
|
|
} else { |
63
|
|
|
|
|
|
|
#push scalars and hashes as is |
64
|
0
|
|
|
|
|
0
|
push @$result, $val; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
10
|
|
|
|
|
35
|
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
|
5012
|
my ($self) = @_; |
76
|
|
|
|
|
|
|
|
77
|
159
|
|
|
|
|
256
|
my $val = join(' ', @{$self->as_flat_array}); |
|
159
|
|
|
|
|
333
|
|
78
|
159
|
|
|
|
|
275
|
my $indent = undef; |
79
|
159
|
|
|
|
|
485
|
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
|
|
|
|
391
|
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
|
|
|
|
|
604
|
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
|
102
|
my ($self) = @_; |
97
|
|
|
|
|
|
|
|
98
|
21
|
|
|
|
|
52
|
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
|
22
|
my ($self) = @_; |
105
|
|
|
|
|
|
|
|
106
|
11
|
|
|
|
|
20
|
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; |