Hi guys,

I am writing a sub to determine the file type of a file like the unix command file does (by using magic files). There are 3 major CPAN modules available for doing this:

  1. File::Type
  2. File::MimeInfo::Magic
  3. File::MMagic

File::Type as it says was made to overcome the limitation of File::MimeInfo but it fails to check even basic files. For e.g. it identifies a text files as application/octet-stream.

File::MMagic is based on some apache software to do its job. I found it better than File::Type as in it does not screw up identifying text files. But for video files, it identifies them as application/octet stream, Identifies mp3 files as application/octet-stream (File::Type identifies mp3 as audio/mp3)

File::MimeInfo::Magic is a derivative of File::MimeInfo to use magic for determining file types. If magic fails we have the option of falling back on the way MimeInfo checks (using freedesktop database). I found it better than the other two as in it passed the simple text file checks, identifies videos (checked for .flv and .m2v) and audio as audio/mpeg

Here is a comparison of their outputs:

File::Type video1.flv - application/octet-stream File::MimeInfo::Magic video1.flv - application/x-flash-video File::MMagic video1.flv - application/octet-stream File::Type fdcheck - application/x-executable-file File::MimeInfo::Magic fdcheck - application/x-executable File::MMagic fdcheck - application/octet-stream File::Type filetypes.pl - application/x-perl File::MimeInfo::Magic filetypes.pl - application/x-perl File::MMagic filetypes.pl - x-system/x-unix; executable /usr/bin/perl + script text File::Type latex-setup.tgz - application/x-gzip File::MimeInfo::Magic latex-setup.tgz - application/x-gzip File::MMagic latex-setup.tgz - application/x-gzip File::Type mail3.eml - message/rfc822 File::MimeInfo::Magic mail3.eml - message/rfc822 File::MMagic mail3.eml - message/rfc822 File::Type video.m2v - application/octet-stream File::MimeInfo::Magic video.m2v - video/x-msvideo File::MMagic video.m2v - application/octet-stream File::Type utf - application/octet-stream File::MimeInfo::Magic utf - text/plain File::MMagic utf - text/plain File::Type audio.mp3 - audio/mp3 File::MimeInfo::Magic audio.mp3 - audio/mpeg File::MMagic audio.mp3 - application/octet-stream File::Type file.txt - application/octet-stream File::MimeInfo::Magic file.txt - text/plain File::MMagic file.txt - text/plain

The problem is that - I don't know which of them confirms to the MIME types used in email messages. For e.g File::MIMEInfo (and not File::MIMEInfo::Magic) identifies tgz files as application/compressed-tar while my email message has its content type as application/x-gzip.

Which of these modules should one use when creating MIME messages? - i.e. when I build an entity using MIME::Entity "attach" method, I have to give the "Type" which I want to determine using one of these.


In reply to Comparing different MIME type checking modules by saurabh.hirani

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.