package Greenstone::Config; use strict; use warnings; use utf8; use Greenstone::Helpers; # Formats an array sub replacement_array { die "Array handling must be defined by implementation"; } # Determines the value to replace a variable with sub replacement { my ($self, $var) = @_; my $val = $self->{config}->{$var}; die "'$var' was not defined!" unless (defined $val); if (ref $val eq 'ARRAY') { return $self->replacement_array ($val); } else { return $val; } } # Substitutes variables with their value sub subst { my $self = shift; for (@_) { s/@([\w]+)@/$self->replacement($1)/ge; } return @_; } # Reads a config file into the config hashmap sub readconf { my ($self, $conf) = @_; $self->{config} = {} unless exists $self->{config}; open CONF, '<', $conf or die "Failed to open '$conf' for reading: $!"; my $var; while (my $line = ) { if (empty $line or comment $line) { $var = undef; } elsif (defined $var and $line =~ /^\s/) { trim $line; $self->subst ($line); escape $line; push @{$self->{config}->{$var}}, $line; } else { ($var, my $val) = trim (split ":", $line, 2); defined $var and defined $val or die "Invalid variable assignment: '$line'"; if (empty $val) { $self->{config}->{$var} = []; } else { $self->subst ($val); escape $val; $self->{config}->{$var} = $val; } } } close CONF; } 1;