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