Parse::MIME(3)
π¦ Parse::MIME(3) β okay bestie, let's talk mime-types π¦
This is the Parse::MIME(3) manual page from the User Contributed Perl Documentation corner of the universe. It's giving tiny but powerful Perl module energy, and honestly? Slay. We're gonna go through it together π
β¨ NAME β¨
Parse::MIME β parse mime-types, match against media ranges π
π SYNOPSIS π
use Parse::MIME qw( best_match );
print best_match( [ qw( application/xbel+xml text/xml ) ], 'text/*;q=0.5,*/*; q=0.1' );
# text/xml
πΈ DESCRIPTION πΈ
This module provides basic functions for handling mime-types. It can handle matching mime-types against a list of media-ranges. See section 14.1 of the HTTP specification [RFC 2616] for a complete explanation: <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1> π
π INTERFACE π
β οΈ No cap: none of the following functions are exported by default. You can use the :all tag to import all of them into your package:
use Parse::MIME ':all';
π§ parse_mime_type π§
Parses a mime-type into its component parts and returns type, subtype and params, where params is a reference to a hash of all the parameters for the media range:
parse_mime_type 'application/xhtml;q=0.5'
# ( 'application', 'xhtml', { q => 0.5 } )
π¦ parse_media_range π¦
Media-ranges are mime-types with wild-cards and a q quality parameter. This function works just like parse_mime_type, but also guarantees that there is a value for q in the params hash, supplying the default value if necessary.
parse_media_range 'application/xhtml'
# ( 'application', 'xhtml', { q => 1 } )
π parse_media_range_list π
Media-range lists are comma-separated lists of media ranges. This function works just like parse_media_range, but accepts a list of media ranges and returns for all of media-ranges β yes, that's the original phrasing, and it means you get all the parsed media-ranges back π
my @l = parse_media_range_list 'application/xhtml, text/html;q=0.7'
# ( 'application', 'xhtml', { q => 1 }, 'text', 'html', { q => 0.7 } )
π₯ fitness_and_quality_parsed π₯
Find the best match for a given mime-type (passed as the first parameter) against a list of media ranges that have already been parsed by parse_media_range (passed as a flat list). Returns the fitness value and the value of the q quality parameter of the best match, or ( -1, 0 ) if no match was found.
# for @l see above
fitness_and_quality_parsed( 'text/html', @l )
# ( 110, 0.7 )
π quality π
Determines the quality (q) of a mime-type (passed as the first parameter) when compared against a media-range list string. F.ex.:
quality( 'text/html', 'text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5' )
# 0.7
π quality_parsed π
Just like quality, except the second parameter must be pre-parsed by parse_media_range_list.
π best_match π
Choose the mime-type with the highest quality (q) from a list of candidates. Takes an array of supported mime-types as the first parameter and finds the best match for all the media-ranges listed in header, which is passed as the second parameter. The value of header must be a string that conforms to the format of the HTTP Accept header. F.ex.:
best_match( [ qw( application/xbel+xml text/xml ) ], 'text/*;q=0.5,*/*; q=0.1' )
# 'text/xml'
π AUTHOR π
Aristotle Pagaltzis <pagaltzis@gmx.de> π β author main character energy, fr fr.
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2018 by Aristotle Pagaltzis.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
xoxo bestie π this page is from perl v5.42.0 and dated 2022-08-11 β footer info successfully folded into a cute sign-off, periodt π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Parse-MIME 1.006-bp160.1.11 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms