Perl command or module like Linux “file” command

走远了吗. 提交于 2019-12-07 17:58:43

问题


I have a script that downloads files but these files do not have any information about them before they are downloaded. While writing for Linux I have just called qx{ 'file ' . $filename } to see if it is a JPEG image an if not delete it. However, I am now trying to rewrite to a platform independent and pure-Perl form. I have turned all the calls to system{ 'curl', $image_website } to LWP::UserAgent calls and I was hoping that there is some way to replace calls to file with something as well.


回答1:


File::Type CPAN module can help you - its description is "determine file type using magic" which is what Unix type command does.

my $ft = File::Type->new();
my $type_from_file = $ft->checktype_filename($file);

Another option is File::LibMagic (Perl wrapper for libmagic; file-4.x or file-5.x)



来源:https://stackoverflow.com/questions/4788542/perl-command-or-module-like-linux-file-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!