Config-File-Simple

 view release on metacpan or  search on metacpan

lib/Config/File/Simple.pm  view on Meta::CPAN

    croak "The variable '$variable' has got special characters!" if $self->has_special_characters($variable);
    croak "The configuration file '$self->{'file'}' doesn't exist!" if ! -e $self->{'file'};

    open my $CONFIG, "<", $self->{'file'} or croak "Can't open file '$self->{'file'}': $!";
    while(my $line = <$CONFIG>) { # Now we parse the config file and search for the variable
        chomp $line;
        $line =~ s/ [^\\] \# .* //xms; # We don't need the comments
        $line =~ s/ ^ \s+ //xms; # Delete all space at the beginnig
        $line =~ s/ \s+ $//xms; # Delete all space at the end
        next if $line !~ / ^ $variable \s* = /xms; # Is this the right variable?
        $value = (split m/=/, $line)[1]; # We need the value
        $value =~ s/ ^ \s+ //xms; # Delete all space at the beginnig of the value
        $value =~ s/ \s+ $ //xms; # Delete all space at the end of the value
        $value =~ s/\\#/#/g; # Unescape the escaped hashs
    }
    
    close $CONFIG or croak "Can't close file '$self->{'file'}': $!";
    return $value;
}

sub multiple_read {

lib/Config/File/Simple.pm  view on Meta::CPAN

    open my $CONFIG, "<", $self->{'file'} or croak "Can't open file '$self->{'file'}': $!";
    while(my $line = <$CONFIG>) {
        chomp $line;
        $line =~ s/ [^\\] \# .* //xms; # Delete all comments
        $line =~ s/ ^ \s+ //xms; # Delete all space at the beginnig
        $line =~ s/ \s+ $//xms; # Delete all space at the end

        foreach my $variable (@variables) {
            next if $line !~ / ^ $variable \s* = /xms; # Is this the right variable?
            my $value; # This value will be added to the hash: $values{$variable} = $value
            $value = (split m/=/, $line)[1]; # We need the value, not the variable
            $value =~ s/ ^ \s+ //xms; # Delete all space at the beginnig of the value
            $value =~ s/ \s+ $ //xms; # Delete all space at the end of the value
            $value =~ s/\\#/#/g; # Unescape the escaped hashs
            $values{$variable} = $value;
        }
    }

    close $CONFIG or croak "Can't close file '$self->{'file'}': $!";
    return %values;
}



( run in 0.779 second using v1.01-cache-2.11-cpan-71847e10f99 )