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 π
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
- fedora 44: perl-LWP-MediaTypes 6.05-1.fc44 Β· (GPL-1.0-or-later OR Artistic-1.0-Perl) AND CC0-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms