Corion has asked for the wisdom of the Perl Monks concerning the following question:

Again, I turn to you in search of a name:

Fed up with the existing solutions to determining the MIME type of a file using the magic numbers, I wrote a module to parse the XML database that is distributed by the freedesktop.org initiative at https://www.freedesktop.org/wiki/Software/shared-mime-info/.

This module allows an application to use its own rules for file type recognition independent of what other rules the operating system may have installed. Also, you gain independence of needing tools like autoconf being available for your platform.

The "problem" I have is coming up with a good name for this module. In the same problem space, there already are:

Currently, my file is not API-compatible with either of them (yet), so I'm not feeling exactly like using one of the above namespaces.

My current bad candidates are:

  1. File::MIME::Magic - my current favourite, has both, "MIME" and "Magic" in the name, which are the two search terms to use when finding the mime type of a file
  2. File::Magic - a great name, but squats on a not too bad namespace
  3. File::Magic::FreeDesktop - leaves File::Magic open for other improvements
  4. File::Magic::XML - somehow suggests that we only look at XML files?!
  5. File::MimeInfo::SharedMimeInfoXML - confusing because I don't implement the File::MimeInfo API

While it's always fun to write my own modules, if you know of other fairly general file type modules, feel free to also mention them here. There are some type-specific modules for audio and image files, which may be enough, depending on your application.

Update: The current API is the following:

my $mime = File::MimeInfo::SharedMimeInfoXML->new(); for my $file (@ARGV) { print sprintf "%s: %s\n", $file, $_->mime_type for $mime->mimetypes($file); };
  • Comment on Looking for a name (Magic number MIME information on file from freedesktop.org database)
  • Select or Download Code

Replies are listed 'Best First'.
Re: Looking for a name (Magic number MIME information on file from freedesktop.org database)
by Perlbotics (Archbishop) on Sep 17, 2016 at 15:18 UTC

    How about MIME::Detect which could implement a generic API or a driver (like DBI) that can interface to various backends like the ones you already listed?
    MIME::Detect::SharedMimeInfoXML could then be one of many backends.

    my $mime_detector = MIME::Detect->new( driver => 'auto' ); could auto-select an available backend.
    my $mime_detector = MIME::Detect->new( driver => 'SharedMimeInfoXML' ); would ask for a specific driver explicitly.

    More work needs to be done for the API to cover most of the existing functionality, though. However, work already done by others can be preserved and made available in a generic way.

    Leaving the FILE:: namespace aside would also allow to broaden the application of MIME-detection to streams (i.e. using IO::Handle), strings, etc.

      At least File::MimeInfo and my module already support filehandles and in-memory streams as well, so that part is covered. I like the idea of leaving File:: out, as it shortens the module name.

      Personally, I've stopped trying to create "generic" modules together with the first implementation, as normally it turned out that my first implementation remained the only implementation and no plugins or other drivers turned up. But using MIME::Detect as the API class and leaving the door open for others to add backends sounds good.

Re: Looking for a name (Magic number MIME information on file from freedesktop.org database)
by RichardK (Parson) on Sep 17, 2016 at 13:54 UTC

    I like the call out to FreeDesktop but as the git package for that is called shared-mime-info another possibility is :-

    File::Magic::SharedMimeInfo
Re: Looking for a name (Magic number MIME information on file from freedesktop.org database)
by ww (Archbishop) on Sep 17, 2016 at 12:33 UTC