Perl module for 'file' command?

On Linux system, I can use a command file

to determine the file type.

Is there a module perl

that encapsulates this command?

+3


source to share


2 answers


If you know you will be in Unix common sense, you can simply make a system call file

.

If you need an independent implementation, there are several available on CPAN . Probably closest to file

File :: MMagic . This is its own implementation, so it will work on any system, but might not act exactly the same as 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

      

+4


source


File :: MMagic inline magic is so short that it's useless. Avoid.

Use File :: LibMagic instead , this is best.



$ perl -mFile::LibMagic -MDDS -E \
    'say Dump(File::LibMagic->new
        ->info_from_filename("Startopia EULA English.docx"))'
$HASH1 = {
    description  => 'Microsoft Word 2007+',
    encoding     => 'binary',
    mime_type    => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    mime_with_encoding => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=binary'
};

      

0


source







All Articles