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.


In reply to Re: convert a Unicode file to Ansi, using filehandle.. by ikegami
in thread convert a Unicode file to Ansi, using filehandle.. by nico7nibor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.