Domanda

I have a question here. Can the parameter in the INI file be change? I know that the value can be change though. But i wonder if the parameter can be change also instead of the value. I show you, for an example, here is the INI file

[section 1]
parameter1 = value1
parameter2 = value2

Instead of changing the value, is there any perl module or anything can change the parameter1 and parameter2 and leave the value1 and value2 unchange. If we want to read the value we just use this right :

my $file = "file directory";
my $Config = Config::Tiny->read($file);

my $read = $Config->{"section 1"}->{"parameter1"};

But how to only change the parameter1 and parameter2

Can this be done? I mean using perl programming. If yes, can anybody show me an example on how to do this. Its for my assignment though. Thank you in advance.

È stato utile?

Soluzione

This is very simple using Config::Tiny but there is no way to rename a value directly - you have to copy the value to an item with the new name and then delete the old one.

This short Perl program demonstrates. It changes parameter1 to newparam1 and parameter2 to newparam2. The new config is written to a different file to facilitate testing, but you will probably want to override the original file.

use strict;
use warnings;

use Config::Tiny;

my $cfg = Config::Tiny->read('file.cfg');

#print $cfg->{'section 1'}{parameter1};

my $section1 = $cfg->{'section 1'};

$section1->{newparam1} = $section1->{parameter1};
delete $section1->{parameter1};

$section1->{newparam2} = $section1->{parameter2};
delete $section1->{parameter2};

$cfg->write('newfile.cfg');

output

[section 1]
newparam1=value1
newparam2=value2
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top