Hi wise monks,

I'd like to use File::MMagic to discriminate between different file types. What I basically need is text, image and "others", i.e. the first part of the MIME type from the start to the / separator. Unfortunately, File::MMagic returns invariably application/octet-stream for image file.

The first argument I gave to File::MMagic is my system "magic" file but File::MMagic result is different from system command file.

I know file is using a compiled version of the system "magic" file and I suppose both formats are equivalent (i.e. they contain the same magic numbers).

I am also sure File::MMagic method new has correctly taken note of the argument because of a bug (see File::MMagic bug?).

This behaviour has already been reported in a different context (see Comparing different MIME type checking modules). I'm not asking here for the "best" MIME type, but only for a coarse indication of the kind of data in a file. My grief is that application/octet-stream is way too coarse and that I could get a finer approximation allowed by the system "magic" file.

It looks to me File::MMagic reverts to its internal table when methods checktype_* are invoked.

1. How can I assert File::MMagic is really using the designated magic file?

2. What is a simple reliable way of classifying files as text, image and "others"?

Note: I don't want to use `file thefile_to_test` as it involves process forking, pipe connection, etc. which would negatively impact performance.


In reply to File::MMagic returns application/octet-stream for most files by ajlittoz

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.