++ed by:

2 PAUSE users
3 non-PAUSE users.

Casey West


File::Type - Determine a file's contents by looking at the name and contents


   use File::Type qw( get_type type_2_mime ) ;

   File::Type::load_magic( "type_file" ) ;
   File::Type::load_magic( \@type_defs ) ;

   my $file_type = get_type( "foo.pl" ) ;

   print type_2_mime( $file_type ) ;


A perl module that acts a lot like the traditional Unix file command, but using regular expressions to do the job.

File types are defined in a data structure that's passed in or in a file that contains such a data structure. Default file types are defined in the module, so you don't need to load_magic() in some cases.



Adds more types to the current magic database.




Adds the contents of a mime types file to the current magic database.


load_magic() takes either a file name or a reference to an array and sets up the internal data structures needed by get_type() and type_2_mime(). See the source code for the module for more information on the data structure required.

The types included with this module are not that comprehensive, since Safari needs to know about very few of them. Submissions of new and better recognizers are appreciated.


get_type() does three levels of check and returns the result of the first sucessful check.

get_type() first stats the file, then looks at it's extension, then looks inside the file using regular expressions. Since perl5 regular expressions are pretty darn comprehensive, this should allow complete emulation of the magic files used by the Unix file command as well as the language identification heuristics.

If a second argument is provided, it will be used as the file's contents, and the file will not be opened. The contents must be from the beginning of the file for most binary file types, and should be for most text file types. As much data as is feasible should be provided.


Takes the result from a get_type call and returns the corresponding mime_type.


The format of the magic data structure is:

      'file type' => [  # reported when a match is found
           'long type'    # Unix find-like description
           'mime type',   # used to translate file type to mime type
         name_test,     # the test applied when only the file name is known
         guts_test_1,   # the first test applied if the file name test fails.
         guts_test_2,   # the second test applied if guts_test_1 fails
      'another type' => [

See file_type for a description of the testing algorithm.

Primitive tests

These functions may be used in the magic data structure as complete tests or as part of other tests.

The text / binary primitives only test the file state once and cache the results.


Aborts all testing by dieing with the message passed.


Prints a message if debugging is enabled.


Returns 1 if the file name has an extension matching any of the arguments


Returns 1 if the file is text, 0 if it is not.


Returns 1 if the file is not text, 0 if it is.


Returns a score based on where and how many of the words or regular expression arguments match. This is the routine used internally when a word list or a regular expression is used in the magic structure.


Returns 1 if the file name matches the regular expression or strings passed in.


Returns -1 if the file is not text (according to -T), 0 if it is. This is used to disqualify a type for a file without scoring the file, since 0 means 'can't tell', and -1 means it's not that type.


Returns -1 if the file is text (according to -T), 0 otherwise.


Barrie Slaymaker

1 POD Error

The following errors were encountered while parsing the POD:

Around line 451:

You forgot a '=back' before '=head2'