1File::MimeInfo(3) User Contributed Perl Documentation File::MimeInfo(3)
2
3
4
6 File::MimeInfo - Determine file type from the file name
7
9 use File::MimeInfo;
10 my $mime_type = mimetype($file);
11 my $mime_type2 = mimetype('test.png');
12
14 This module can be used to determine the mime type of a file. It tries
15 to implement the freedesktop specification for a shared MIME database.
16
17 For this module shared-mime-info-spec 0.13 was used.
18
19 This package only uses the globs file. No real magic checking is used.
20 The File::MimeInfo::Magic package is provided for magic typing.
21
22 If you want to determine the mimetype of data in a memory buffer you
23 should use File::MimeInfo::Magic in combination with IO::Scalar.
24
25 This module loads the various data files when needed. If you want to
26 hash data earlier see the "rehash" methods below.
27
29 The method "mimetype" is exported by default. The methods "inodetype",
30 "globs", "extensions", "describe", "mimetype_canon" and "mimetype_isa"
31 can be exported on demand.
32
34 "new()"
35 Simple constructor to allow Object Oriented use of this module. If
36 you want to use this, include the package as "use File::MimeInfo
37 ();" to avoid importing sub "mimetype()".
38
39 "mimetype($file)"
40 Returns a mimetype string for $file, returns undef on failure.
41
42 This method bundles "inodetype" and "globs".
43
44 If these methods are unsuccessful the file is read and the mimetype
45 defaults to 'text/plain' or to 'application/octet-stream' when the
46 first ten chars of the file match ascii control chars (white spaces
47 excluded). If the file doesn't exist or isn't readable "undef" is
48 returned.
49
50 "inodetype($file)"
51 Returns a mimetype in the 'inode' namespace or undef when the file
52 is actually a normal file.
53
54 "globs($file)"
55 Returns a mimetype string for $file based on the filename and
56 filename extensions. Returns undef on failure. The file doesn't
57 need to exist.
58
59 Behaviour in list context (wantarray) is unspecified and will
60 change in future releases.
61
62 "default($file)"
63 This method decides whether a file is binary or plain text by
64 looking at the first few bytes in the file. Used to decide between
65 "text/plain" and "application/octet-stream" if all other methods
66 have failed.
67
68 The spec states that we should check for the ascii control chars
69 and let higher bit chars pass to allow utf8. We try to be more
70 intelligent using perl utf8 support.
71
72 "extensions($mimetype)"
73 In list context, returns the list of filename extensions that map
74 to the given mimetype. In scalar context, returns the first
75 extension that is found in the database for this mimetype.
76
77 "describe($mimetype, $lang)"
78 Returns a description of this mimetype as supplied by the mime info
79 database. You can specify a language with the optional parameter
80 $lang, this should be the two letter language code used in the xml
81 files. Also you can set the global variable $File::MimeInfo::LANG
82 to specify a language.
83
84 This method returns undef when no xml file was found (i.e. the
85 mimetype doesn't exist in the database). It returns an empty string
86 when the xml file doesn't contain a description in the language you
87 specified.
88
89 Currently no real xml parsing is done, it trusts the xml files are
90 nicely formatted.
91
92 "mimetype_canon($mimetype)"
93 Returns the canonical mimetype for a given mimetype. Deprecated
94 mimetypes are typically aliased to their canonical variants. This
95 method only checks aliases, doesn't check whether the mimetype
96 exists.
97
98 Use this method as a filter when you take a mimetype as input.
99
100 "mimetype_isa($mimetype)"
101 "mimetype_isa($mimetype, $mimetype)"
102 When give only one argument this method returns a list with
103 mimetypes that are parent classes for this mimetype.
104
105 When given two arguments returns true if the second mimetype is a
106 parent class of the first one.
107
108 This method checks the subclasses table and applies a few rules for
109 implicit subclasses.
110
111 "has_mimeinfo_database()"
112 Check if there are mimeinfo database files available; returns 1 on
113 success. If you don't have the shared-mime-info package installed
114 or not in the PATH or @File::MimeInfo::DIRS does not contain
115 database directories, you will not get the successful reply.
116
117 New in version 0.30.
118
119 "rehash()"
120 Rehash the data files. Glob information is preparsed when this
121 method is called.
122
123 If you want to by-pass the XDG basedir system you can specify your
124 database directories by setting @File::MimeInfo::DIRS. But normally
125 it is better to change the XDG basedir environment variables.
126
127 "rehash_aliases()"
128 Rehashes the mime/aliases files.
129
130 "rehash_subclasses()"
131 Rehashes the mime/subclasses files.
132
134 This module throws an exception when it can't find any data files, when
135 it can't open a data file it found for reading or when a subroutine
136 doesn't get enough arguments. In the first case you either don't have
137 the freedesktop mime info database installed, or your environment
138 variables point to the wrong places, in the second case you have the
139 database installed, but it is broken (the mime info database should
140 logically be world readable).
141
143 Make an option for using some caching mechanism to reduce init time.
144
145 Make "describe()" use real xml parsing ?
146
148 Perl versions prior to 5.8.0 do not have the ':utf8' IO Layer, thus for
149 the default method and for reading the xml files utf8 is not supported
150 for these versions.
151
152 Since it is not possible to distinguish between encoding types (utf8,
153 latin1, latin2 etc.) in a straightforward manner only utf8 is
154 supported (because the spec recommends this).
155
156 This module does not yet check extended attributes for a mimetype.
157 Patches for this are very welcome.
158
159 This module uses the FreeDesktop.org shared mime info database. On your
160 desktop linux this is typically pre-installed so it's not a problem. On
161 your server you can install the shared-mime-info package via apt or dnf
162 or apk or whatnot.
163
164 To install on macOS, you can install it like this:
165
166 brew install shared-mime-info
167
169 Jaap Karssenberg <pardus@cpan.org> Maintained by Michiel Beijen
170 <michiel.beijen@gmail.com>
171
173 Copyright (c) 2003, 2012 Jaap G Karssenberg. All rights reserved. This
174 program is free software; you can redistribute it and/or modify it
175 under the same terms as Perl itself.
176
178 File::BaseDir, File::MimeInfo::Magic, File::MimeInfo::Applications,
179 File::MimeInfo::Rox
180
181 related CPAN modules
182 File::MMagic
183
184 freedesktop specifications used
185 <http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec>,
186 <http://www.freedesktop.org/wiki/Specifications/basedir-spec>,
187 <http://www.freedesktop.org/wiki/Specifications/desktop-entry-spec>
188
189 freedesktop mime database
190 <http://www.freedesktop.org/wiki/Software/shared-mime-info>
191
192
193
194perl v5.34.0 2021-07-22 File::MimeInfo(3)