File Coverage

lib/Rex/File/Parser/Ini.pm
Criterion Covered Total %
statement 8 43 18.6
branch 0 12 0.0
condition 0 3 0.0
subroutine 3 8 37.5
pod 0 4 0.0
total 11 70 15.7


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::File::Parser::Ini;
6              
7 1     1   20 use v5.12.5;
  1         3  
8 1     1   18 use warnings;
  1         3  
  1         57  
9              
10             our $VERSION = '1.14.3'; # VERSION
11              
12 1     1   10 use Data::Dumper;
  1         4  
  1         620  
13              
14             sub new {
15 0     0 0   my $that = shift;
16 0   0       my $proto = ref($that) || $that;
17 0           my $self = {@_};
18              
19 0           bless( $self, $proto );
20              
21 0           return $self;
22             }
23              
24             sub get {
25 0     0 0   my ( $self, $section, $key ) = @_;
26              
27 0 0         unless ( exists $self->{"__data"}->{$section} ) {
28 0           die("$section not found");
29             }
30              
31 0 0         unless ( exists $self->{"__data"}->{$section}->{$key} ) {
32 0           die("$key not found in $section");
33             }
34              
35 0           return $self->{"__data"}->{$section}->{$key};
36             }
37              
38             sub get_sections {
39 0     0 0   my ($self) = @_;
40 0           return keys %{ $self->{"__data"} };
  0            
41             }
42              
43             sub read {
44 0     0 0   my ($self) = @_;
45 0           $self->{"__data"} = $self->_read_file;
46             }
47              
48             sub _read_file {
49 0     0     my ($self) = @_;
50              
51 0           my $data = {};
52              
53 0           my $section;
54 0           open( my $fh, "<", $self->{"file"} );
55 0           while ( my $line = <$fh> ) {
56 0           chomp $line;
57 0 0         next if ( $line =~ m/^\s*?;/ );
58 0 0         next if ( $line =~ m/^\s*?$/ );
59              
60 0 0         if ( $line =~ m/^\[(.+)\]$/ ) {
61 0           $section = $1;
62 0           $data->{$section} = {};
63 0           next;
64             }
65              
66 0 0         if ($section) {
67 0           my ( $key, $val ) = split( /=/, $line, 2 );
68 0           $val =~ s/^\s+|\s+$//g;
69 0           $key =~ s/^\s+|\s+$//g;
70              
71 0           $data->{$section}->{$key} = $val;
72             }
73             }
74 0           close($fh);
75              
76 0           return $data;
77             }
78              
79             1;