Mojolicious::Types(3)
π Mojolicious::Types β MIME types, but make it cute
Okay bestie, so this whole page is from the User Contributed Perl Documentation section of the manual, and it's all about Mojolicious::Types β the lil module that keeps track of what MIME type goes with what file extension. Let's get into it π
β¨ NAME β¨
Mojolicious::Types β MIME types. That's it, that's the whole vibe. It's the MIME type keeper π«Ά
πΈ SYNOPSIS πΈ
Here's the whole thing in action, look how easy:
use Mojolicious::Types;
my $types = Mojolicious::Types->new;
$types->type(foo => 'text/foo');
say $types->type('foo');
New object, teach it that foo means text/foo, then ask it right back. Iconic behaviour, no notes π
π¦ DESCRIPTION π¦
Mojolicious::Types manages MIME types for Mojolicious. That's literally its whole job and it does it slayfully.
So here's the default mapping it ships with β and yes, all of these come pre-loaded:
appcache -> text/cache-manifest
atom -> application/atom+xml
bin -> application/octet-stream
css -> text/css
gif -> image/gif
gz -> application/x-gzip
htm -> text/html
html -> text/html;charset=UTF-8
ico -> image/x-icon
jpeg -> image/jpeg
jpg -> image/jpeg
js -> application/javascript
json -> application/json;charset=UTF-8
mp3 -> audio/mpeg
mp4 -> video/mp4
ogg -> audio/ogg
ogv -> video/ogg
pdf -> application/pdf
png -> image/png
rss -> application/rss+xml
sse -> text/event-stream
svg -> image/svg+xml
ttf -> font/ttf
txt -> text/plain;charset=UTF-8
webm -> video/webm
woff -> font/woff
woff2 -> font/woff2
xml -> application/xml,text/xml
zip -> application/zip
The most common ones are already defined, so you can just vibe and start using them πβ¨
π ATTRIBUTES π
Mojolicious::Types implements the following attributes. There's only the one but she's doing her best π
π mapping
my $mapping = $types->mapping;
$types = $types->mapping({png => ['image/png']});
MIME type mapping. Read it, or hand it a whole new hashref and it just takes over. Main character energy fr fr.
π₯ METHODS π₯
Mojolicious::Types inherits all methods from Mojo::Base and implements the following new ones. Free Mojo::Base methods, we love to see it π«Ά
β content_type
$types->content_type(Mojolicious::Controller->new, {ext => 'json'});
Detect the MIME type for a Mojolicious::Controller object β but only unless a "Content-Type" response header has already been set, okay? Like, don't overwrite, that's rude. And if no better alternative could be found, it defaults to using "application/octet-stream", which is basically the "idk man" of MIME types π
These options are currently available:
extβ file extension to get the MIME type for. Example:ext => 'json'fileβ file path to get the MIME type for. Example:file => 'foo/bar.png'
π detect
my $exts = $types->detect('text/html, application/json;q=9');
Detect file extensions from an "Accept" header value. Give it the header string, get extensions back. Lowkey magic β¨
# List detected extensions prioritized
say for @{$types->detect('application/json, text/xml;q=0.1', 1)};
That second argument there gets you the detected extensions prioritized, so you can loop over them in order. Chef's kiss π
π file_type
my $type = $types->file_type('foo/bar.png');
Get the MIME type for a file path. Hand it a path, it hands you the type. Simple, clean, iconic π
π§ type
my $type = $types->type('png');
$types = $types->type(png => 'image/png');
$types = $types->type(json => ['application/json', 'text/x-json']);
Get or set MIME types for a file extension. You can pass one string, or an arrayref of several β just know that the alternatives are only used for detection, so the first one is the one that actually represents the thing π
π SEE ALSO π
Mojolicious, Mojolicious::Guides, and <https://mojolicious.org> β go read them, they're literally so good, iykyk πΈ
And that's the whole page bestie!! This one's from perl v5.42.0, dated 2026-07-15, and it's the Mojolicious::Types(3) page from the User Contributed Perl Documentation. Go forth and MIME responsibly πβ¨π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Mojolicious 9.480.0-bp160.2.1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms