Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

blech (1597)

  (email not shown publicly)

Journal of blech (1597)

Wednesday August 27, 2003
06:36 AM

My First Module: File::Type

[ #14344 ]

I've finally uploaded a module to CPAN.

File::Type uses magic to find out the type of a file, like file(1). Of course, the observant will point out that this merely duplicates the functionality of File::MMagic (and, the eager CPAN watcher will note, File::MimeInfo).

However, there are a couple of problems with both of those modules. File::MimeInfo doesn't use magic at all yet, but merely glob patterns. It also doesn't ship with the magic file it references.

File::MMagic is the canonical solution to the problem, and it does include a magic file (inlined into itself). Each time File::MMagic is run, it recreates the rules it needs to match by parsing that inlined magic. This means it uses seeks within a __DATA__ block, which causes problems within mod_perl. File::Type, by contrast, uses a seperate module (File::Type::Builder, which is included in the distribution, as is a magic file) to generate Perl code which is then included in File::Type itself. This means that the module can be very simple indeed, and it's safe to use under mod_perl.

However, as the version number should warn you, File::Type is still quite young. While it creates a few hundred rules from the magic file, there are still a hundred or so that it ignores because I haven't written the code generators for their pattern types. It's fine for many types of file (as the included tests show), though.

After releasing the main module (and the obligatory 'doh, need to bugfix' release), I thought it'd be fun to see if I could make an equivalent to File::Find::Rule::MMagic, which adds the ability to filter File::Find::Rule searches by mime type. It appeared on CPAN as File::Find::Rule::Type yesterday evening.


The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • I was under the impression that the problem with "mod_perl" and __DATA__ was only related to CGI scripts. In particular, running them under Apache::Registry, where they will cause the wrapper that Registry puts around them to be missed, leading to compilation failures. Ordinary perl modules should be ok though.


    • Aha, that may well be the case. However, it seems that the hoops that File::MMagic has to jump through while creating its in-memory rules have other problems in forking or mod_perl enviroments, as, for example, this RT bug [] shows.

      Because File::Type is pregenerated, and therefore made out of really simple rules - internally it's merely a string of if statements, the most complex thing in them being a very simple pack - it's unlikely to exhibit any problems.