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 "rehash()"
112 Rehash the data files. Glob information is preparsed when this
113 method is called.
114
115 If you want to by-pass the XDG basedir system you can specify your
116 database directories by setting @File::MimeInfo::DIRS. But normally
117 it is better to change the XDG basedir environment variables.
118
119 "rehash_aliases()"
120 Rehashes the mime/aliases files.
121
122 "rehash_subclasses()"
123 Rehashes the mime/subclasses files.
124
126 This module throws an exception when it can't find any data files, when
127 it can't open a data file it found for reading or when a subroutine
128 doesn't get enough arguments. In the first case you either don't have
129 the freedesktop mime info database installed, or your environment
130 variables point to the wrong places, in the second case you have the
131 database installed, but it is broken (the mime info database should
132 logically be world readable).
133
135 Make an option for using some caching mechanism to reduce init time.
136
137 Make "describe()" use real xml parsing ?
138
140 Perl versions prior to 5.8.0 do not have the ':utf8' IO Layer, thus for
141 the default method and for reading the xml files utf8 is not supported
142 for these versions.
143
144 Since it is not possible to distinguish between encoding types (utf8,
145 latin1, latin2 etc.) in a straightforward manner only utf8 is
146 supported (because the spec recommends this).
147
148 This module does not yet check extended attributes for a mimetype.
149 Patches for this are very welcome.
150
152 Jaap Karssenberg <pardus@cpan.org> Maintained by Michiel Beijen
153 <michiel.beijen@gmail.com>
154
156 Copyright (c) 2003, 2012 Jaap G Karssenberg. All rights reserved. This
157 program is free software; you can redistribute it and/or modify it
158 under the same terms as Perl itself.
159
161 File::BaseDir, File::MimeInfo::Magic, File::MimeInfo::Applications,
162 File::MimeInfo::Rox
163
164 related CPAN modules
165 File::MMagic
166
167 freedesktop specifications used
168 <http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec>,
169 <http://www.freedesktop.org/wiki/Specifications/basedir-spec>,
170 <http://www.freedesktop.org/wiki/Specifications/desktop-entry-spec>
171
172 freedesktop mime database
173 <http://www.freedesktop.org/wiki/Software/shared-mime-info>
174
175
176
177perl v5.30.1 2020-01-30 File::MimeInfo(3)