开发者

How to test if PerlIO::fse works on file test operators?

开发者 https://www.devze.com 2023-04-07 16:54 出处:网络
#!/usr/local/bin/perl use warnings; use strict; use utf8; use Encode qw(encode); my $dir = \'/data/Delibes, Léo\';
#!/usr/local/bin/perl
use warnings;
use strict;
use utf8;
use Encode qw(encode);
my $dir = '/data/Delibes, Léo';

if ( -d $dir ) {
    print "OK\n";
}

if ( -d encode 'utf8', $dir ) {
    print "OK\n";
}

This prints 2 times OK; I suppose this is because Perl stores $dir internally as utf8.

I there a way to check if PerlIO::fse has an effect on the filetest operators as long as perl and the filesystem stores in utf8?


Edit:

#!/usr/local/bin/perl
use warnings;
use 5.014;
binmode STDOUT, 'utf8';
use utf8;

my $dir1 = 'é';
my $dir2 = chr(0xE9);

opendir my $dh1, $dir1 or warn '1: ', $!;
say "OK1" if -d $dir1;
opendir my $dh2, $dir2 or warn '2: ', $!;
say "OK2" if -d $dir2;

utf8::upgrade( $dir1 );
utf8::upgrade( $dir2 );

opendir my $dh3, $dir1 or warn '3: ', $!;
say "OK3" if -d $dir1;
opendir my $dh4, $dir2 or warn '4: ', $!;
say "OK4" if -d $dir2;

# 开发者_StackOverflow中文版OK1
# 2: Datei oder Verzeichnis nicht gefunden at ./temp1.pl line 12.
# OK3
# OK4

Maybe I have not exactly understood how PerlIO::fse works - in this example I can't see any effect from PerlIO::fse:

#!/usr/local/bin/perl
use warnings;
use 5.014;
binmode STDOUT, 'utf8';
use utf8;
use PerlIO::fse 'utf-8';

my $dir1 = 'é';
my $dir2 = chr(0xE9);

opendir my $dh1, $dir1 or warn '1: ', $!;
say "OK1" if -d $dir1;
opendir my $dh2, $dir2 or warn '2: ', $!;
say "OK2" if -d $dir2;

# OK1                                                                                       
# 2: Datei oder Verzeichnis nicht gefunden at ./temp1.pl line 13. 


Test with:

my $file_name = chr(0xE9);  # e acute.
utf8::downgrade($file_name);

my $file_name = chr(0xE9);
utf8::upgrade($file_name);

The first will produce junk in a UTF-8 locale if it's not UTF-8 encoded first.

The second will produce junk in other locales if it's not encoded first.

(They're suppose to be the same, but there's a bug in most/all builtins that take file names, the same bug that's preventing you from testing it properly.)

0

精彩评论

暂无评论...
验证码 换一张
取 消