1Parse::MIME(3) User Contributed Perl Documentation Parse::MIME(3)
2
3
4
6 Parse::MIME - Parse mime-types, match against media ranges
7
9 use Parse::MIME qw( best_match );
10 print best_match( [ qw( application/xbel+xml text/xml ) ], 'text/*;q=0.5,*/*; q=0.1' );
11 # text/xml
12
14 This module provides basic functions for handling mime-types. It can
15 handle matching mime-types against a list of media-ranges. See section
16 14.1 of the HTTP specification [RFC 2616] for a complete explanation:
17 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1>
18
20 None of the following functions are exported by default. You can use
21 the ":all" tag to import all of them into your package:
22
23 use Parse::MIME ':all';
24
25 parse_mime_type
26 Parses a mime-type into its component parts and returns type, subtype
27 and params, where params is a reference to a hash of all the parameters
28 for the media range:
29
30 parse_mime_type 'application/xhtml;q=0.5'
31 # ( 'application', 'xhtml', { q => 0.5 } )
32
33 parse_media_range
34 Media-ranges are mime-types with wild-cards and a "q" quality
35 parameter. This function works just like "parse_mime_type", but also
36 guarantees that there is a value for "q" in the params hash, supplying
37 the default value if necessary.
38
39 parse_media_range 'application/xhtml'
40 # ( 'application', 'xhtml', { q => 1 } )
41
42 parse_media_range_list
43 Media-range lists are comma-separated lists of media ranges. This
44 function works just like "parse_media_range", but accepts a list of
45 media ranges and returns for all of media-ranges.
46
47 my @l = parse_media_range_list 'application/xhtml, text/html;q=0.7'
48 # ( 'application', 'xhtml', { q => 1 }, 'text', 'html', { q => 0.7 } )
49
50 fitness_and_quality_parsed
51 Find the best match for a given mime-type (passed as the first
52 parameter) against a list of media ranges that have already been parsed
53 by "parse_media_range" (passed as a flat list). Returns the fitness
54 value and the value of the "q" quality parameter of the best match, or
55 "( -1, 0 )" if no match was found.
56
57 # for @l see above
58 fitness_and_quality_parsed( 'text/html', @l )
59 # ( 110, 0.7 )
60
61 quality
62 Determines the quality ("q") of a mime-type (passed as the first
63 parameter) when compared against a media-range list string. F.ex.:
64
65 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' )
66 # 0.7
67
68 quality_parsed
69 Just like "quality", except the second parameter must be pre-parsed by
70 "parse_media_range_list".
71
72 best_match
73 Choose the mime-type with the highest quality ("q") from a list of
74 candidates. Takes an array of supported mime-types as the first
75 parameter and finds the best match for all the media-ranges listed in
76 header, which is passed as the second parameter. The value of header
77 must be a string that conforms to the format of the HTTP "Accept"
78 header. F.ex.:
79
80 best_match( [ qw( application/xbel+xml text/xml ) ], 'text/*;q=0.5,*/*; q=0.1' )
81 # 'text/xml'
82
84 Aristotle Pagaltzis <pagaltzis@gmx.de>
85
87 This software is copyright (c) 2018 by Aristotle Pagaltzis.
88
89 This is free software; you can redistribute it and/or modify it under
90 the same terms as the Perl 5 programming language system itself.
91
92
93
94perl v5.32.1 2021-01-27 Parse::MIME(3)