File Coverage

blib/lib/MIME/Field/ContType.pm
Criterion Covered Total %
statement 16 23 69.5
branch 3 8 37.5
condition n/a
subroutine 7 11 63.6
pod 3 8 37.5
total 29 50 58.0


line stmt bran cond sub pod time code
1             package MIME::Field::ContType;
2              
3              
4             =head1 NAME
5              
6             MIME::Field::ContType - a "Content-type" field
7              
8              
9             =head1 DESCRIPTION
10              
11             A subclass of Mail::Field.
12              
13             I
14             Instead, ask Mail::Field for new instances based on the field name!
15              
16              
17             =head1 SYNOPSIS
18              
19             use Mail::Field;
20             use MIME::Head;
21              
22             # Create an instance from some text:
23             $field = Mail::Field->new('Content-type',
24             'text/HTML; charset="US-ASCII"');
25              
26             # Get the MIME type, like 'text/plain' or 'x-foobar'.
27             # Returns 'text/plain' as default, as per RFC 2045:
28             my ($type, $subtype) = split('/', $field->type);
29              
30             # Get generic information:
31             print $field->name;
32              
33             # Get information related to "message" type:
34             if ($type eq 'message') {
35             print $field->id;
36             print $field->number;
37             print $field->total;
38             }
39              
40             # Get information related to "multipart" type:
41             if ($type eq 'multipart') {
42             print $field->boundary; # the basic value, fixed up
43             print $field->multipart_boundary; # empty if not a multipart message!
44             }
45              
46             # Get information related to "text" type:
47             if ($type eq 'text') {
48             print $field->charset; # returns 'us-ascii' as default
49             }
50              
51              
52             =head1 PUBLIC INTERFACE
53              
54             =over 4
55              
56             =cut
57              
58             require 5.001;
59 21     21   20076 use strict;
  21         40  
  21         473  
60 21     21   576 use MIME::Field::ParamVal;
  21         31  
  21         163  
61 21     21   12437 use vars qw($VERSION @ISA);
  21         36  
  21         6970  
62              
63             @ISA = qw(MIME::Field::ParamVal);
64              
65             # The package version, both in 1.23 style *and* usable by MakeMaker:
66             $VERSION = "5.507";
67              
68             # Install it:
69             bless([])->register('Content-type');
70              
71             #------------------------------
72             #
73             # Basic access/storage methods...
74             #
75             sub charset {
76 1 50   1 0 5 lc(shift->paramstr('charset', @_)) || 'us-ascii'; # RFC 2045
77             }
78             sub id {
79 0     0 0 0 shift->paramstr('id', @_);
80             }
81             sub name {
82 17     17 0 51 shift->paramstr('name', @_);
83             }
84             sub number {
85 0     0 0 0 shift->paramstr('number', @_);
86             }
87             sub total {
88 0     0 0 0 shift->paramstr('total', @_);
89             }
90              
91              
92             #------------------------------
93              
94             =item boundary
95              
96             Return the boundary field. The boundary is returned exactly
97             as given in the C field; that is, the leading
98             double-hyphen (C<-->) is I prepended.
99              
100             (Well, I exactly... from RFC 2046:
101              
102             (If a boundary appears to end with white space, the white space
103             must be presumed to have been added by a gateway, and must be deleted.)
104              
105             so we oblige and remove any trailing spaces.)
106              
107             Returns the empty string if there is no boundary, or if the boundary is
108             illegal (e.g., if it is empty after all trailing whitespace has been
109             removed).
110              
111             =cut
112              
113             sub boundary {
114 3     3 1 16 my $value = shift->param('boundary', @_);
115 3 50       16 defined($value) || return '';
116 3         11 $value =~ s/\s+$//; # kill trailing white, per RFC 2046
117 3         11 $value;
118             }
119              
120             #------------------------------
121              
122             =item multipart_boundary
123              
124             Like C, except that this will also return the empty
125             string if the message is not a multipart message. In other words,
126             there's an automatic sanity check.
127              
128             =cut
129              
130             sub multipart_boundary {
131 0     0 1 0 my $self = shift;
132 0         0 my ($type) = split('/', $self->type);
133 0 0       0 return '' if ($type ne 'multipart'); # not multipart!
134 0         0 $self->boundary; # okay, return the boundary
135             }
136              
137             #------------------------------
138              
139             =item type
140              
141             Try real hard to determine the content type (e.g., C<"text/plain">,
142             C<"image/gif">, C<"x-weird-type">, which is returned
143             in all-lowercase.
144              
145             A happy thing: the following code will work just as you would want,
146             even if there's no subtype (as in C<"x-weird-type">)... in such a case,
147             the $subtype would simply be the empty string:
148              
149             ($type, $subtype) = split('/', $head->mime_type);
150              
151             If the content-type information is missing, it defaults to C<"text/plain">,
152             as per RFC 2045:
153              
154             Default RFC 2822 messages are typed by this protocol as plain text in
155             the US-ASCII character set, which can be explicitly specified as
156             "Content-type: text/plain; charset=us-ascii". If no Content-Type is
157             specified, this default is assumed.
158              
159             B under the "be liberal in what we accept" principle, this routine
160             no longer syntax-checks the content type. If it ain't empty,
161             just downcase and return it.
162              
163             =cut
164              
165             sub type {
166 28 50   28 1 141 lc(shift->paramstr('_', @_)) || 'text/plain'; # RFC 2045
167             }
168              
169             #------------------------------
170              
171             =back
172              
173              
174             =head1 NOTES
175              
176             Since nearly all (if not all) parameters must have non-empty values
177             to be considered valid, we just return the empty string to signify
178             missing fields. If you need to get the I underlying value,
179             use the inherited C method (which returns undef if the
180             parameter is missing).
181              
182             =head1 SEE ALSO
183              
184             L, L
185              
186             =head1 AUTHOR
187              
188             Eryq (F), ZeeGee Software Inc (F).
189             Dianne Skoll (dfs@roaringpenguin.com) http://www.roaringpenguin.com
190              
191             =cut
192              
193             1;
194              
195              
196