Perl模块的“文件”命令?

CJ7*_*CJ7 4 perl file-type file

在Linux系统上,我可以使用file命令来检测文件的类型。

是否有perl封装此命令的模块?

Sch*_*ern 5

如果您知道要使用健全的Unix,则可以对进行系统调用file

如果您需要独立的实现,则CPAN上有几种可用的实现。可能最接近的fileFile :: MMagic。这是它自己的实现,因此它可以在任何系统上运行,但行为可能不完全相同file

$ head test.pl
#!/usr/bin/env perl

use strict;
use warnings;

$ file test.pl
test.pl: a /usr/bin/env perl script text executable, ASCII text

$ perl -wlE 'use File::MMagic; $mm = File::MMagic->new; say $mm->checktype_filename(shift)' test.pl
x-system/x-unix;  executable /usr/bin/env script text
Run Code Online (Sandbox Code Playgroud)