# SimplestConfig.pm # SimplestConfig->new('filename') returns an object whose attributes # (simple values only at this time) will be stored in a file named # filename under the form attribute=value and as such may be usefull # to manage simple config files. use strict; use warnings; package SimplestConfig; sub new { my ($class, $filename) = @_; my $self = {_FileName_ => $filename}; bless $self, $class; if (open CFG, "<$filename") { while () { /^\s*(\S.*?)\s*=\s*(.*?)\s*$/ and $self->{$1} = $2; } close CFG; } else { warn "Can't read $filename, creating it\n"; } return $self; } sub DESTROY { my ($self) = @_; open CFG, ">$self->{_FileName_}" or die "Can't write $self->{_FileName_}\n"; delete $self->{_FileName_}; print CFG map "$_=$self->{$_}\n", sort keys(%{$self}); close CFG; } 1;