in reply to convert a Unicode file to Ansi, using filehandle..
use strict; use warnings;! It'll find numerous errors including the fact that you use $Temp without ever assigning a value to it.
m//g in scalar context is almost always an error. It is here.
Your pattern will accidentally match keys "Named", "Names", etc.
use strict; use warnings; my $Profile = "C:\\registry.reg"; # unicode file my $old_name = 'Foo'; my $new_name = 'Bar'; my @reg_file; { open(my $fh, '<:raw:perlio:encoding(UTF-16):crlf', $Profile) or die("Can't open file $Profile: $!\n"); @reg_file = <$fh> } for (@reg_file) { s/ ^ ( \[ HKEY_LOCAL_MACHINE\\SOFTWARE\\ ) $old_name ( (?:\\.*)? \] ) /$1$new_name$2/x; } { open(my $fh, '>:raw:perlio:encoding(UTF-16le):crlf', $Profile) or die("Can't overwrite file $Profile: $!\n"); print $fh "\x{FEFF}", @reg_file; }
The weird stuff on the open line is to handle CRLF properly with UTF-16
Using UTF-16 instead of the real encoding (UTF-16le) removes the byte order mark, if I remember correctly. That's the odd character I print out at the end.
Ok, fine, I think the file is actually encoded using UCS-2le, not UTF-16le, but it's a subset of UTF-16le, so all's good.
|
|---|