File Coverage

lib/Rex/Helper/INI.pm
Criterion Covered Total %
statement 46 55 83.6
branch 16 20 80.0
condition 3 3 100.0
subroutine 4 4 100.0
pod 0 1 0.0
total 69 83 83.1


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Helper::INI;
6              
7 1     1   15 use v5.12.5;
  1         3  
8 1     1   5 use warnings;
  1         2  
  1         55  
9              
10             our $VERSION = '1.14.2.3'; # TRIAL VERSION
11              
12 1     1   54 BEGIN { String::Escape->use('string2hash'); }
13              
14             sub parse {
15 1     1 0 7 my (@lines) = @_;
16 1         3 my $ini;
17              
18             my $section;
19 1         2 for (@lines) {
20 39         52 chomp;
21 39         77 s/\n|\r//g;
22              
23 39 100       137 (/^#|^;|^\s*$/) && (next);
24              
25 27 100 100     111 if ( /^\[(.*)\]/ && !/^\[(\d+((?:,)|(?:\.\.))*)+(\/\d+)*\]/ ) {
26              
27             # check for inheritance
28 9         20 $section = $1;
29 9         52 $ini->{$section} = {};
30              
31 9 100       27 if ( $section =~ /
32 1         4 delete $ini->{$section};
33 1         3 my @inherit = split( /
34 1         10 s/^\s*|\s*$//g for @inherit;
35 1         2 $section = shift @inherit;
36              
37 1         2 for my $is (@inherit) {
38 1         2 for my $ik ( keys %{ $ini->{$is} } ) {
  1         4  
39 5         12 $ini->{$section}->{$ik} = $ini->{$is}->{$ik};
40             }
41             }
42             }
43              
44 9         17 next;
45             }
46              
47 18         46 my ( $key, $val ) = split( /[= ]/, $_, 2 );
48 18 50       99 $key =~ s/^\s*|\s*$//g if $key;
49 18 100       44 $val =~ s/^\s*|\s*$//g if $val;
50              
51 18         22 my @splitted;
52 18 100       32 if ( !$val ) {
53 17         22 $val = $key;
54 17         28 @splitted = ($key);
55             }
56              
57             # commented out due to #184
58             else {
59             #@splitted = split(/\./, $key);
60 1         6 @splitted = ($key);
61             }
62              
63 18         25 my $ref = $ini->{$section};
64 18         23 my $last = pop @splitted;
65 18         30 for my $sub (@splitted) {
66              
67 0 0       0 unless ( exists $ini->{$section}->{$sub} ) {
68 0         0 $ini->{$section}->{$sub} = {};
69             }
70              
71 0         0 $ref = $ini->{$section}->{$sub};
72             }
73              
74             # include other group
75 18 100       32 if ( $key =~ m/^\@(.*)/ ) {
76 1         1 for my $ik ( keys %{ $ini->{$1} } ) {
  1         5  
77 3         8 $ini->{$section}->{$ik} = $ini->{$1}->{$ik};
78             }
79 1         4 next;
80             }
81              
82 17 50       29 if ( $val =~ m/\$\{(.*)\}/ ) {
83 0         0 my $var_name = $1;
84 0         0 my $ref = $ini;
85 0         0 my @splitted = split( /\./, $var_name );
86 0         0 for my $s (@splitted) {
87 0         0 $ref = $ref->{$s};
88             }
89              
90 0         0 $val = $ref;
91             }
92              
93 17 100       26 if ( $val =~ m/=/ ) {
94 1         8 $val = { string2hash($val) };
95             }
96              
97 17         226 $ref->{$last} = $val;
98              
99             }
100              
101 1         5 return $ini;
102             }
103              
104             1;