in reply to Re^2: Win32 registry export
in thread Win32 registry export

First of all thank you Anonymous Monk. You advise pointed me to the right direction. At the moment I'm able to get the output in a format of regedit.exe tool. There is only one problem: the tool writes the files as Unicode UTF-16LE with byte-order-marks (BOM). There is no problem to write a file in unicode and also the BOM sequence came up very fast:
open my $fh, '>:encoding(UTF-16LE)', 'wwe-perl.reg'; print {$fh} "\x{FEFF}"; print {$fh} "Windows Registry Editor Version 5.00\n\n"; say {$fh} "[HKLM\\SOFTWARE\\WWE]"; close $fh;
But I'm stuck with a problem I can't find a solution: every end of line has a byte sequence of "00 0D 00 0A" at regedit output and "00 0D 0A 00" at my script output. I don't know what is wrong and how to change this. With the current output I the file looks like a number of Chinese characters. Everything works fine if don't add "\n" to my output (the editor displays the string "Windows Registry Editor Version 5.00" and Unicode BOM as file format. Thank you for any help!

Replies are listed 'Best First'.
Re^4: Win32 registry export
by Anonymous Monk on Jun 17, 2010 at 12:37 UTC
    Check your layers
    print "layers ", PerlIO::get_layers($fh), "\n";
    You probably need raw
    open my $fh, '>:raw:encoding(....
    See PerlIO and related docs for more
      Thank you again, :raw:encoding(UTF-16LE) didn't help. This produces 00 0A 00 as newline. It looks like correct Unicode but not same as need. Here is the output of a different setting using the PerlIO::get_layers($fh):
      layers :raw:encoding(UTF-16LE):crlf:via(File::BOM): unixencoding(UTF-16LE)utf8crlfutf8viautf8 layers :encoding(UTF-16LE): unixcrlfencoding(UTF-16LE)utf8 :raw:encoding(UTF-16LE): unixencoding(UTF-16LE)utf8
      I found the solution in the activestate forum: http://community.activestate.com/forum-topic/creating-unicode-utf-16le. You need to use File::BOM and open the file as following:
      open my $fh, '>:raw:encoding(UTF-16LE):crlf:via(File::BOM)', 'file.reg +';
      The same problem is described there. I don't know what this line is doing, any comments of unicode and Perl insiders are welcome. I assume this writes newline using this File::BOM module and this way the byte order is correct. My script is now writing pretty fine output. I exported the whole HKLM/Software and had only few differences. This is a value I can live with. If I export only the sub-tree of my interest the file is completely identical to the output of regedit.exe. Thank you for your help.
        Just raed the post of ikegami where an other solution was mentioned: U-DOS to DOS file conversion '>:raw:perlio:encoding(UTF-16le):crlf' tested this with the code:
        open my $fh4, '>:raw:perlio:encoding(UTF-16le):crlf', 'file4.txt' or d +ie "$!"; print {$fh4} "\x{FEFF}"; print $fh4 "AB\n"; print $fh4 "CD\n";
        and it looks like newlines are good here.