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.
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.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.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.
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.
Barrie Slaymaker
Hey! The above document had some coding errors, which are explained below: