Perl6有没有像Perl5 -T file test这样的东西来判断文件是否是文本文件?
发布于 2019-04-15 07:48:48
没有内置任何东西,但是有一个模块Data::TextOrBinary可以做到这一点。
use Data::TextOrBinary;
say is-text('/bin/bash'.IO); # False
say is-text('/usr/share/dict/words'.IO); # True
发布于 2019-04-15 06:54:44
这是一个启发式的has not been translated to Perl 6。您可以简单地在UTF8 (或ASCII码)中读取它来执行相同的操作:
given slurp("read-utf8.p6", enc => 'utf8') -> $f {
say "UTF8";
}
(将read-utf8.p6替换为要检查的文件的名称)
发布于 2019-04-17 06:48:44
我们可以通过以下代码使用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
}
https://stackoverflow.com/questions/55683746
复制相似问题