Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Accent file names issue

by ruimelo73 (Novice)
on Sep 21, 2017 at 21:47 UTC ( [id://1199854]=note: print w/replies, xml ) Need Help??


in reply to Accent file names issue

I used the suggestions given by "Anonymous Monk" and I managed to find a solution to handle correctly string variables with direct text. Here's the code I created to test some common stuff:

use strict; use warnings; use utf8; use feature 'unicode_strings'; use charnames ':full'; our $base = "c:\\users\\someuser\\documents"; our $dp = "$base\\documentação"; { my $new_dp = $dp; my $success = utf8::decode($new_dp); print "isutf8 test 1: "; if (utf8::is_utf8($dp)) { print "ok\n"; } else { print "nope\n"; } print "isutf8 test 2: "; if (utf8::is_utf8($new_dp)) { print "ok\n"; } else { print "nope\n"; } print "-d test 1: "; if (-d $dp) { print "ok\n"; } else { print "nope\n"; }; print "-d test 2: "; if (-d $new_dp) { print "ok\n"; } else { print "nope\n"; }; my $dh; print "opendir test 1: "; if (opendir($dh, $dp)) { print "ok\n"; close($dh); } else { print "nope\n"; } print "opendir test 2: "; if (opendir($dh, $new_dp)) { print "ok\n"; close($dh); } else { print "nope\n"; } my $buf; print "dir test 1: "; $buf = `dir /b $dp 2> nul`; chop($buf); if ($buf ne "") { print "ok\n"; } else { print "nope\n"; } print "dir test 2: "; $buf = `dir /b $new_dp 2> nul`; chop($buf); if ($buf ne "") { print "ok\n"; } else { print "nope\n"; } my $r; print "dir test 1: "; $r = system("dir $dp > nul 2> nul"); if ($r == 0) { print "ok\n"; } else { print "nope\n"; } print "dir test 2: "; $r = system("dir $new_dp > nul 2> nul"); if ($r == 0) { print "ok\n"; } else { print "nope\n"; } }

The output was:

C:\...>perl perlmonks2.pl isutf8 test 1: ok isutf8 test 2: nope -d test 1: nope -d test 2: ok opendir test 1: nope opendir test 2: ok dir test 1: nope dir test 2: ok dir test 1: nope dir test 2: ok C:\...>

I will have to do some tests in other computers and with UNC file paths, but it seems that this is it. Now that it is working I can even live with it, but I really hope that future developing on Unicode and Perl can reduce the obscure use of lines of code for doing such simple things.

You guys are great, I wish all the best for who ever try to help.

Experience is the mother of all things, from it we kwnow radicaly the truth -- Duarte Pacheco Pereira, portuguese adventurer and the secret discoverer of America

Replies are listed 'Best First'.
Re^2: Accent file names issue
by vr (Curate) on Sep 22, 2017 at 00:40 UTC

    Note: you only get away succeed with what you did, because Portuguese uses Latin1, and because utf8::decode modifies its argument even on failure (which is surprising).

    The utf8::decode expects octets, but you're trying to feed it with proper Unicode. Of course it fails (check return value), but nevertheless, for whatever vague reason, it converts the utf8 string to latin1-encoded string, -- only when, it seems, such conversion is possible.

    use strict; use warnings; use utf8; my $x1 = my $x2 = 'ç'; die if utf8::decode($x2); use Devel::Peek; Dump $x1; Dump $x2;

    >perl test170921.pl SV = PV(0xe1af88) at 0xd23560 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK,UTF8) PV = 0xd6b458 "\303\247"\0 [UTF8 "\x{e7}"] CUR = 2 LEN = 10 COW_REFCNT = 1 SV = PV(0xe1af58) at 0xd22fc0 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0xd6b5a8 "\347"\0 CUR = 1 LEN = 10

    So this encoding from utf8 to latin1 (even though you did it inadvertently with call to decode) is just a special narrow case of what I wrote in this thread about encoding to your system codepage when reaching outside from Perl, and decoding whatever you fetch back.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1199854]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found