in reply to Update config file parameters

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

Replies are listed 'Best First'.
Re^2: Update config file parameters
by afoken (Chancellor) on Jan 07, 2016 at 21:38 UTC
    You can do inplace editing of files:

    But that introduces a race condition. The problem is documented in perlrun:

    -i[extension]
    specifies that files processed by the <> construct are to be edited in-place. It does this by renaming the input file, opening the output file by the original name, and selecting that output file as the default for print() statements.

    The race: Instance 1 is started, reads the configuration file, then starts rewriting by renaming the configuration file and creating a new, still empty (or partly written) configuration file. Instance 2 is started, and reads an empty (or partly written) configuration file.

    How to get rid of that problem:

    • Use a (separate) lock file to synchronise access to the configuration file. Only the instance that can successfully lock the lock file may read or write the configuration file.
    • Use an atomic operation to change the configuration file. The only way I know is to rewrite the configuration file to a new, temporary file, and to rename that file to the original configuration file name.

    (Note that safely creating temporary files is another problem: mkstemp(3) and tmpfile(3) should be safe. mktemp(3) and tmpnam(3) aren't. File::Temp has safe and unsafe functions.)

    Alexander

    (And yes, the problem of "disappearing configuration data" has bitten me once, and it was quite hard to find.)

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)