如果您知道要使用健全的Unix,则可以对进行系统调用file。
如果您需要独立的实现,则CPAN上有几种可用的实现。可能最接近的file是File :: 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)