Joel Berger
Joel Berger

Reputation: 20280

Perl command or module like Linux "file" command

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.

Upvotes: 3

Views: 1282

Answers (1)

DVK
DVK

Reputation: 129481

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)

Upvotes: 6

Related Questions