You can do inplace editing of files:

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

To get inplace editing, you cannot read a file like this:

while(my $line = $INFILE)

You have to read a file using the diamond operator: <>. You cannot have anything between the angle brackets. The diamond operator reads from @ARGV (or STDIN if @ARGV is empty). So, you either have to set @ARGV to the file name, or enter the file name on the command line. @ARGV contains all the arguments entered on the command line.

When you enable inplace editing, perl creates a new file, redirects STDOUT to the new file, i.e. print() and say() go to the new file; and when you are done, perl saves a copy of the original file using the original file name with a .bak extension, then perl renames the new file to the original file name. If you don't want to save a copy of the original file, then write:

{ local $^I = ""; ... ... }

Here's a sample run:

~/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

In reply to Re: Update config file parameters by 7stud
in thread Update config file parameters by pavan.gup

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.