MIME::Types(3pm) | User Contributed Perl Documentation | MIME::Types(3pm) |
MIME::Types - Definition of MIME types
MIME::Types is an Exporter
use MIME::Types; my $mt = MIME::Types->new(...); # MIME::Types object my $type = $mt->type('text/plain'); # MIME::Type object my $type = $mt->mimeTypeOf('gif'); my $type = $mt->mimeTypeOf('picture.jpg'); my @types = $mt->httpAccept('text/html, application/json;q=0.1')
MIME types are used in many applications (for instance as part of e-mail and HTTP traffic) to indicate the type of content which is transmitted. or expected. See RFC2045 at https://www.ietf.org/rfc/rfc2045.txt
Sometimes detailed knowledge about a mime-type is need, however this module only knows about the file-name extensions which relate to some filetype. It can also be used to produce the right format: types which are not registered at IANA need to use 'x-' prefixes.
This object administers a huge list of known mime-types, combined from various sources. For instance, it contains all IANA types and the knowledge of Apache. Probably the most complete table on the net!
If your program uses fork (usually for a daemon), then you want to have the type table initialized before you start forking. So, first call
my $mt = MIME::Types->new;
Later, each time you create this object (you may, of course, also reuse the object you create here) you will get access to the same global table of types.
-Option --Default db_file <installed source> only_complete <false> only_iana <false> skip_extensions <false>
[2.10] This parameter can be globally overruled via the "PERL_MIME_TYPE_DB" environment variable, which may be needed in case of PAR or other tricky installations. For PAR, you probably set this environment variable to "inc/lib/MIME/types.db"
In your program you have to decide: the first time that you call the creator ("new") determines whether you get the full or the partial information.
Please inform the maintainer of this module when registered types are missing. Before version MIME::Types version 1.14, a warning was produced when an unknown IANA type was added. This has been removed, because some people need that to get their application to work locally... broken applications...
In some cases, more than one type is known for a certain filename extension. In that case, the preferred one is taken (for an unclear definition of preference)
example: use of mimeTypeOf()
my $types = MIME::Types->new; my $mime = $types->mimeTypeOf('gif'); my $mime = $types->mimeTypeOf('picture.jpg'); print $mime->isBinary;
[before 2.00] One type may be described more than once. Different extensions may be in use for this type, and different operating systems may cause more than one "MIME::Type" object to be defined. In scalar context, only the first is returned.
Ill-formated typenames are ignored. On equal qualities, the order is kept. See RFC2616 section 14.1
example:
my @types = $types->httpAccept('text/html, application/json;q=0.9');
As second parameter, you pass a LIST of types you @have to offer. Those need to be MIME::Type objects. The preferred type will get selected. When none of these are accepted by the client, this will return "undef". It should result in a 406 server response.
example:
my $accept = $req->header('Accept'); my @have = map $mt->type($_), qw[text/plain text/html]; my @ext = $mt->httpAcceptBest($accept, @have);
example:
use HTTP::Status ':constants'; use File::Glob 'bsd_glob'; # understands blanks in filename my @filenames = bsd_glob "$imagedir/$fnbase.*; my $accept = $req->header('Accept'); my ($fn, $mime) = $mt->httpAcceptSelect($accept, @filenames); my $code = defined $mime ? HTTP_NOT_ACCEPTABLE : HTTP_OK;
The next functions are provided for backward compatibility with MIME::Types versions [0.06] and below. This code originates from Jeff Okamoto okamoto@corp.hp.com and others.
TYPE can be a full type name (contains '/', and will be matched in full), a partial type (which is used as regular expression) or a real regular expression.
example: use of function by_suffix()
use MIME::Types 'by_suffix'; my ($mediatype, $encoding) = by_suffix('image.gif'); my $refdata = by_suffix('image.gif'); my ($mediatype, $encoding) = @$refdata;
This module is part of MIME-Types distribution version 2.24, built on December 28, 2022. Website: http://perl.overmeer.net/CPAN/
Copyrights 1999-2022 by [Mark Overmeer <markov@cpan.org>]. For other contributors see ChangeLog.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://dev.perl.org/licenses/
2022-12-30 | perl v5.36.0 |