Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed perl inplace editing of files not on command line
    primarykey
    data
    text
    <p>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, then changes the database password for the database account. The list of config 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:</p> <pre><code>BEGIN { $^I = '.oldPW'; } # Enable in-place editing ... foreach (@{$Services{$request}{'files'}}) { my $filename = $Services{$request}{'configDir'} . '/' . $_; print "Processing ${filename}\n"; open CONFIGFILE, '+&lt;', $filename or warn $!; while (&lt;CONFIGFILE&gt;) { s/$oldPass/$newPass/; print; } close CONFIGFILE; } </code></pre> <p>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.</p> <blockquote> <p></p> </blockquote> <p><strong>Update:</strong> I found what I was looking for on <a href="http://www.perlmonks.org/?node_id=106461">perlmonks</a>. You can use a local ARGV inside the loop to do inplace editing in the normal perl way. The above loop now looks like:</p> <pre><code>foreach (@{$Services{$request}{'files'}}) { my $filename = $Services{$request}{'configDir'} . '/' . $_; print "Processing ${filename}\n"; { local @ARGV = ( $filename); while (&lt;&gt;) { s/$oldPass/$newPass/; print; } } } </code></pre> <p>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.</p> <p>Thanks for the helpful suggestions on Tie::File, I'd probably go that way if doing this over. The config files I'm editing are never more than a few Kbytes in length, so a Tie wouldn't use too much memory.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload