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