Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello

What is wrong in the following code if I read a path from a utf-8 file (a ini file) and use this path to create a folder with non-latin characters on Windows (Perl 5.28) with Win32::CreateDirectory? If the same path is hardcoded, the folder is created correctly!

configStartup.ini is encoded in UTF-8 and has the following one line: ForceDirectoryData=D:/Test Ршзефф 号召力打了

use Win32; use utf8; my $configFile='configStartup.ini'; my $UserDataReadFromFile; open my $FH, '<:encoding(UTF-8)', $configFile; while (my $line = <$FH>) { if ($line =~/^ForceDirectoryData/){ (my $tag, $UserDataReadFromFile) = split(/=/, $line); } } close $FH; #1 the following doesn't work (dies) Win32::CreateDirectory ($UserDataReadFromFile) or die; #2 it works if hardcoded my $UserDataHardCoded='D:/Test &#1056;&#1096;&#1079;&#1077;&#1092;&#10 +92; &#21495;&#21484;&#21147;&#25171;&#20102;'; Win32::CreateDirectory ($UserDataHardCoded) or die;

Replies are listed 'Best First'.
Re: Utf-8 path from file
by vr (Curate) on Feb 26, 2019 at 20:44 UTC

    You forgot to chomp. If, as 1st line in a while block, there's chomp $line;, then you are OK. +You'd better die $^E instead of just die.

    OTOH (digressing, I know), if instead of code after the "#1" line you do

    use Win32::LongPath; testL 'e', $UserDataReadFromFile or mkdirL $UserDataReadFromFile or die $^E;

    there's no need in chomping, previously. It may look as a nice free bonus, but it's misleading. See 5 lines, comment says "strip off volume", but effectively it strips newline too, because a dot doesn't match newline there. There can be ways to bypass that fragment, therefore unaware user will be bitten. Thus, always chomp.

Re: Utf-8 path from file
by Anonymous Monk on Feb 26, 2019 at 19:55 UTC
    It should be: my $UserDataHardCoded='D:/Test Ршзефф 号召力打了';