LWP::MediaTypes(3)

πŸ’– LWP::MediaTypes β€” bestie edition πŸ’–

okay so this is a page from the User Contributed Perl Documentation, and it's all about LWP::MediaTypes β€” the little module that guesses media type for a file or a URL. πŸ¦‹


✨ NAME ✨

LWP::MediaTypes β€” guess media type for a file or a URL πŸ’…


🌸 SYNOPSIS 🌸

use LWP::MediaTypes qw(guess_media_type);
$type = guess_media_type("/tmp/foo.gif");

that's literally it, bestie. two lines and you're slaying. ✨


πŸŽ€ DESCRIPTION πŸŽ€

This module hands you functions for dealing with media types and encodings β€” and yes, media types are also known as MIME types, iykyk. 🫢

The mapping from file extensions to media types is defined by the media.types file. But if the ~/.media.types file exists, that one gets used instead. And for backwards compatibility we will also look for ~/.mime.types. πŸ—‚οΈ

The following functions are exported by default:

πŸ‘‘ guess_media_type( $filename )

πŸ‘‘ guess_media_type( $uri )

πŸ‘‘ guess_media_type( $filename_or_object, $header_to_modify )

This function tries to guess media type and encoding for a file, or for objects that support the path or filename method β€” like URI or File::Temp objects, iconic behavior honestly. When an object does not support either method, it will be stringified to determine the filename. 😌

It returns the content type, which is a string like "text/html". And in array context it also returns any content encodings applied (in the order used to encode the file). You can pass a URI object reference instead of the file name, no big deal. πŸ’β™€οΈ

If the type can not be deduced from looking at the file name, then guess_media_type() will let the -T Perl operator take a look. If this works (and -T returns a TRUE value) then we return text/plain as the type, otherwise we return application/octet-stream as the type. mystery solved bestie πŸ”

The optional second argument should be a reference to a HTTP::Headers object, or any object that implements the $obj->header method in a similar way. When it is present, the values of the 'Content-Type' and 'Content-Encoding' will be set for this header. πŸ’–

πŸ’œ media_suffix( $type, ... )

This function will return all suffixes that can be used to denote the specified media type(s). Wildcard types can be used. In a scalar context it will return the first suffix found. Examples:

@suffixes = media_suffix('image/*', 'audio/basic');
$suffix = media_suffix('text/html');

The following functions are only exported by explicit request β€” so you gotta ask nicely, periodt. πŸ™ˆ

🧚 add_type( $type, @exts )

Associate a list of file extensions with the given media type. Example:

add_type("x-world/x-vrml" => qw(wrl vrml));

🧚 add_encoding( $type, @ext )

Associate a list of file extensions with an encoding type. Example:

add_encoding("x-gzip" => "gz");

🧚 read_media_types( @files )

Parse media types files and add the type mappings found there. Example:

read_media_types("conf/mime.types");

Copyright 1995-1999 Gisle Aas.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. πŸ’–


and that's the whole page, bestie β€” go forth and guess those media types, you absolute icon πŸ‘‘

mwah πŸ’‹ this was perl v5.42.3, dated 2026-09-14, from LWP::MediaTypes(3) β€” slay, goodbye, no cap βœ¨πŸ¦‹πŸ’…

πŸ“¦ where this page comes from