My First Module: File::Type

blech on 2003-08-27T11:36:31

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 MMagic.pm 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.

Enjoy.


mod_perl problems only with cgi scripts

Dom2 on 2003-08-27T13:25:44

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.

-Dom

Re:mod_perl problems only with cgi scripts

blech on 2003-08-27T14:09:29

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.