Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need Perl inplace editing of files not on command line

I have a program that has a number of filenames configured internally. The program edits a bunch of configuration files associated with a database account, and then changes the database password for the database account.

The list of configuration files is associated with the name of the database account via an internal list. When I process these files, I have the following loop in my program:

BEGIN { $^I = '.oldPW'; }  # Enable in-place editing
...
foreach (@{$Services{$request}{'files'}})
{
    my $filename = $Services{$request}{'configDir'} . '/' . $_;
    print "Processing ${filename}\n";
    open CONFIGFILE, '+<', $filename or warn $!;
    while (<CONFIGFILE>)
    {
        s/$oldPass/$newPass/;
        print;
    }
    close CONFIGFILE;
}

The problem is, this writes the modified output to STDOUT, not CONFIGFILE. How do I get this to actually edit inplace? Move the $^I inside the loop? Print CONFIGFILE? I'm stumped.

>

Update: I found what I was looking for on PerlMonks. You can use a local ARGV inside the loop to do inplace editing in the normal Perl way. The above loop now looks like:

foreach (@{$Services{$request}{'files'}})
{
    my $filename = $Services{$request}{'configDir'} . '/' . $_;
    print "Processing ${filename}\n";
    {
        local @ARGV = ( $filename);
        while (<>)
        {
            s/$oldPass/$newPass/;
            print;
        }
    }
}

If it weren't for tacking the configDir on the beginning, I could just toss the whole list into the local @ARGV, but this is efficient enough.

Thanks for the helpful suggestions on Tie::File. I'd probably go that way if doing this over. The configuration files I'm editing are never more than a few  KB in length, so a Tie wouldn't use too much memory.

like image 852
Wexxor Avatar asked May 19 '11 19:05

Wexxor


People also ask

How do I edit a file in Perl?

Open the file in update mode ( "+<" ), read the whole file into an array of lines, change the array, then rewrite the file and truncate it to its current seek pointer.

What is Perl PE?

The perl -pe command. 1. Unix command to replace old date with current date dynamically. 0.

How do you replace a line in a file using Perl?

To insert a line after one already in the file, use the -n switch. It's just like -p except that it doesn't print $_ at the end of the loop, so you have to do that yourself. In this case, print $_ first, then print the line that you want to add. To delete lines, only print the ones that you want.


2 Answers

The recent versions of File::Slurp provide convenient functions, edit_file and edit_file_lines. The inner part of your code would look:

use File::Slurp qw(edit_file);
edit_file { s/$oldPass/$newPass/g } $filename;
like image 133
Grrrr Avatar answered Oct 19 '22 03:10

Grrrr


The $^I variable only operates on the sequence of filenames held in $ARGV using the empty <> construction. Maybe something like this would work:

BEGIN { $^I = '.oldPW'; }  # Enable in-place editing
...

local @ARGV = map {
    $Services{$request}{'configDir'} . '/' . $_ 
} @{$Services{$request}{'files'}};
while (<>) {
   s/$oldPass/$newPass/;

   # print? print ARGVOUT? I don't remember
   print ARGVOUT;
}

but if it's not a simple script and you need @ARGV and STDOUT for other purposes, you're probably better off using something like Tie::File for this task:

use Tie::File;
foreach (@{$Services{$request}{'files'}})
{
    my $filename = $Services{$request}{'configDir'} . '/' . $_;

    # make the backup yourself
    system("cp $filename $filename.oldPW");   # also consider File::Copy

    my @array;
    tie @array, 'Tie::File', $filename;

    # now edit @array
    s/$oldPass/$newPass/ for @array;

    # untie to trigger rewriting the file
    untie @array;
}
like image 31
mob Avatar answered Oct 19 '22 03:10

mob