aartist has asked for the wisdom of the Perl Monks concerning the following question:

I have a config file, with fields as in fieldX:fieldY=value. I have mutiple such rows. I like to have a some sort of module, which can extract the values from this file, put in some sort of data structure ( $hashref or an $object ). When I change the data using $hashref/$object , it should also change in the config file. The changed config file should be written to a new location in directory system without destorying the old one. I do not want to change the order of the config items in the file. Some sort of module pointer would help too.

Thanks for your help.

Replies are listed 'Best First'.
Re: Config file changes
by jffry (Hermit) on Mar 29, 2011 at 22:43 UTC

    Your syntax seems to be parsable by Config::Simple. It has been 4+ years since I have used that module, but it worked very well for me back then.

Re: Config file changes
by ww (Archbishop) on Mar 29, 2011 at 21:26 UTC
    Attaching medium denomination, unmarked and well-used currency would be a good idea.

    PM exists to help you and others learn to program; not to write programs for you.

      As I mentioned, I do not want complete code. I can manage that. I might be missing a config module that can accomplish most of the task. This is a reasonably general task for lots of programming/configuration job as I believe.
Re: Config file changes
by repellent (Priest) on Apr 05, 2011 at 03:57 UTC
    $ cat test.conf fieldX:fieldY=123 fieldA:fieldB=234 $ cat t.pl #!/usr/bin/perl use warnings; use strict; { package MyConfig; use Tie::File; sub new { my ($class, $file) = @_; tie(my @config, "Tie::File", $file) or die("Failed to read ", $file, "\n ", $!); return bless \@config, $class; } sub setting { my $self = shift; my $line = shift; if (3 == grep { defined($_) } @_) { $self->[$line] = "$_[0]:$_[1]=$_[2]"; } else { return split /[:=]/, $self->[$line]; } } sub DESTROY { untie @{ $_[0] } } } use Data::Dumper; my $s = MyConfig->new("test.conf"); my @set1 = $s->setting(0); # at line 1 my @set2 = $s->setting(1); # at line 2 print Dumper \@set1, \@set2; $s->setting(1, "foo", "bar", "777"); # modify line 2 $s->setting(4, "baz", "red", "999"); # add line 5 __END__ $ t.pl $VAR1 = [ 'fieldX', 'fieldY', '123' ]; $VAR2 = [ 'fieldA', 'fieldB', '234' ]; $ cat test.conf fieldX:fieldY=123 foo:bar=777 baz:red=999