The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

File::MimeInfo - Determine file type

SYNOPSIS

  use File::MimeInfo;
  my $mime_type = mimetype($file);

DESCRIPTION

This module can be used to determine the mime type of a file. It tries to implement the freedesktop specification for a shared MIME database.

For this module shared-mime-info-spec 0.11 and basedir-spec 0.5 where used.

Currently only the globs file is used. No real magic checking is used. Although if the file exists and doesn't match any globs, the first line will be checked for ascii control chars.

( See "SEE ALSO" for url's )

EXPORT

The method mimetype is exported by default. The methods inodetype, globs and describe can be exported on demand.

METHODS

new()

Simple constructor to allow Object Oriented use of this module. If you want to use this, use the package as use File::MimeInfo (); to avoid importing sub mimetype.

mimetype($file)

Returns a mime-type string for $file, returns undef on failure.

Currently only globs are supported, for this the file doesn't need to exist. When the globs don't match the file is read and the mime-type defaults to 'text/plain' or to 'application/octet-stream' when the first ten chars of the file match ascii control chars (white spaces excluded). If the file doesn't exist or isn't readable undef is returned.

inodetype($file)

Returns a mimetype in the 'inode' namespace or undef when the file is actually a normal file.

globs($file)

Returns a mime-type string for $file based on the glob rules, returns undef on failure. $file should be stripped of it's directory part, the file doesn't need to exist.

describe($mimetype)

Returns a description of this mimetype as supplied by the mime info database. You can set the global variable $File::MimeInfo::LANG to specify a language, this should be the two letter language code used in the xml files. Returns undef when there seems to be no xml file for this mimetype, this could very well mean the mimetype doesn't exist, it could also mean that the language you specified wasn't found.

Currently no real xml parsing is done, it trust the xml files are nicely formatted.

dirs()

Lists all directories that would be scanned when rehashing, they don't need to exist or need have a "mime" subdir.

The default behaviour can be overloaded by setting global variable @File::MimeInfo::DIRS, this is mainly used for testing purposes.

It lists the least important dir first.

rehash()

Rehash the data files. Glob information is preparsed when this method is called.

ENVIRONMENT

This module uses the following two environment variables when looking for available data file.

Quoting basedir-spec 0.5 :

XDG_DATA_HOME

Defines the base directory relative to which user specific data files should be stored. If $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share is used.

Mime data could be found in $XDG_DATA_HOME/mime.

XDG_DATA_DIRS

Defines the preference-ordered set of base directories to search for data files in addition to the $XDG_DATA_HOME base directory. The directories in $XDG_DATA_DIRS should be seperated with a colon ':'. If $XDG_DATA_DIRS is either not set or empty, a value equal to /usr/local/share/:/usr/share/ should is used.

Mime data could be found in the "mime" subdirs of these dirs.

The order of base directories denotes their importance; the first directory listed is the most important. When the same information is defined in multiple places the information defined relative to the more important base directory takes precedent. The base directory defined by $XDG_DATA_HOME is considered more important than any of the base directories defined by $XDG_DATA_DIRS.

DIAGNOSTICS

This module throws an exception when it can't find any data files, when it can't open a data file it found for reading or when a subroutine doesn't get enough arguments. In the first case youn either don't have the freedesktop mime info database installed, or your environment variables point to the wrong places, in the second case you have the database installed, but it is broken (the mime info database should logically be world readable).

TODO

Make an option for using some caching mechanism to reduce init time.

Make describe do real xml parsing?

BUGS

Non I know of, please mail me when you encounter one.

AUTHOR

Jaap Karssenberg || Pardus [Larus] <pardus@cpan.org<gt>

Copyright (c) 2003 Jaap G Karssenberg. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

File::MMagic

freedesktop specifications used

http://www.freedesktop.org/standards/shared-mime-info-spec/, http://www.freedesktop.org/standards/basedir-spec/

freedesktop mime database

http://www.freedesktop.org/software/shared-mime-info/

other programs using this mime system

http://rox.sourceforge.net

1 POD Error

The following errors were encountered while parsing the POD:

Around line 320:

=over without closing =back