I have some issue with a Perl script. It modifies the content of a file, then reopen it to write it, and in the process some characters are lost. All words starting with ‘%’ are deleted from the file. That’s pretty annoying because the % expressions are variable placeholders for dialog boxes.
Do you have any idea why? Source file is an XML with default encoding
Here is the code:
undef $/; open F, $file or die "cannot open file $file\n"; my $content = <F>; close F; $content =~s{status=["'][\w ]*["']\s*}{}gi; printf $content; open F, ">$file" or die "cannot reopen $file\n"; printf F $content; close F or die "cannot close file $file\n";
You’re using
printfthere and it thinks its first argument is a format string. See theprintfdocumentation for details. When I run into this sort of problem, I always ensure that I’m using the functions correctly. 🙂You probably want just print:
In your example, you don’t need to read in the entire file since your substitution does not cross lines. Instead of trying to read and write to the same filename all at once, use a temporary file:
This also reduces to this command-line program: