use strict;
use warnings;
use 5.020;
my %new_values = (
ONBOOT => 'no',
TYPE => 'Wifi',
);
my $file_name = "data.txt";
read_file($file_name, \%new_values);
sub read_file {
my ($fname, $new_values_href) = @_;
local $^I = ".bak"; #Enable inplace editing for this block only.
local @ARGV = $fname; #Set value for @ARGV for this block only.
while (my $line = <>) {
my ($name, $val) = split /=/, $line;
my $replacement_value = $new_values_href->{$name};
if (defined $replacement_value) {
say "$name=$replacement_value"; #Output goes to file
}
else {
print $line; #Output goes to file
}
}
} #Restore $^I and @ARGV to the values they had before the block
####
while(my $line = $INFILE)
####
{
local $^I = "";
...
...
}
####
~/pperl_programs$ cat data.txt
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
TYPE=Ethernet
IPADDR=10.9.0.200
NETMASK=255.255.0.0
GATEWAY=10.9.1.254
~/pperl_programs$ perl 1.pl
~/pperl_programs$ cat data.txt
DEVICE=eth0
ONBOOT=no
BOOTPROTO=static
TYPE=Wifi
IPADDR=10.9.0.200
NETMASK=255.255.0.0
GATEWAY=10.9.1.254