Perl6是否有类似Perl5 -T file test的东西来判断文件是否是文本文件?
没有任何内置,但有一个模块Data::TextOrBinary这样做。
use Data::TextOrBinary;
say is-text('/bin/bash'.IO); # False
say is-text('/usr/share/dict/words'.IO); # True
那是has not been translated to Perl 6的启发式。你可以简单地用UTF8(或ASCII)读取它来做同样的事情:
given slurp("read-utf8.p6", enc => 'utf8') -> $f {
say "UTF8";
}
(将read-utf8.p6替换为您要检查的文件的名称)
我们可以使用以下代码来使用File :: Type。
use strict;
use warnings;
use File::Type;
my $file = '/path/to/file.ext';
my $ft = File::Type->new();
my $file_type = $ft->mime_type($file);
if ( $file_type eq 'application/octet-stream' ) {
# possibly a text file
}
elsif ( $file_type eq 'application/zip' ) {
# file is a zip archive
}