File Coverage

lib/Mail/Make/Headers/ContentTransferEncoding.pm
Criterion Covered Total %
statement 45 53 84.9
branch 8 14 57.1
condition 3 14 21.4
subroutine 13 18 72.2
pod 6 8 75.0
total 75 107 70.0


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## MIME Email Builder - ~/lib/Mail/Make/Headers/ContentTransferEncoding.pm
3             ## Version v0.2.0
4             ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5             ## Author: Jacques Deguest <jack@deguest.jp>
6             ## Created 2026/03/02
7             ## Modified 2026/03/03
8             ## All rights reserved.
9             ##
10             ## This program is free software; you can redistribute it and/or modify it
11             ## under the same terms as Perl itself.
12             ##----------------------------------------------------------------------------
13             package Mail::Make::Headers::ContentTransferEncoding;
14             BEGIN
15             {
16 9     9   51 use strict;
  9         19  
  9         315  
17 9     9   34 use warnings;
  9         17  
  9         549  
18 9     9   1232 warnings::register_categories( 'Mail::Make' );
19 9     9   42 use parent qw( Module::Generic );
  9         16  
  9         47  
20 9     9   705 use vars qw( $VERSION $EXCEPTION_CLASS %VALID_ENCODINGS );
  9         13  
  9         561  
21 9     9   73 use Mail::Make::Exception;
  9         27  
  9         62  
22             use overload
23             (
24             '""' => 'as_string',
25 314     314   5024 bool => sub{1},
26 9     9   2839 );
  9         17  
  9         170  
27             # RFC 2045 section 6.1 - defined encodings
28 9         62 %VALID_ENCODINGS =
29             (
30             '7bit' => 1,
31             '8bit' => 1,
32             'binary' => 1,
33             'base64' => 1,
34             'quoted-printable' => 1,
35             );
36 9         18 our $EXCEPTION_CLASS = 'Mail::Make::Exception';
37 9         275 our $VERSION = 'v0.2.0';
38             };
39              
40 9     9   49 use strict;
  9         29  
  9         194  
41 9     9   35 use warnings;
  9         21  
  9         4322  
42              
43             sub init
44             {
45 53     53 1 5549 my $self = shift( @_ );
46 53         578 $self->{_encoding} = undef;
47 53         326 $self->{_exception_class} = $EXCEPTION_CLASS;
48             # Module::Generic passes positional args through init() as-is when
49             # _init_strict_use_sub is not set - accept the encoding as first positional arg.
50 53         125 my $encoding = shift( @_ );
51 53 50       234 $self->SUPER::init( @_ ) || return( $self->pass_error );
52 53 50 33     601 if( defined( $encoding ) && length( $encoding ) )
53             {
54 53 100       229 $self->encoding( $encoding ) || return( $self->pass_error );
55             }
56 52         265 return( $self );
57             }
58              
59             sub as_string
60             {
61 0     0 1 0 my $self = shift( @_ );
62 0   0     0 return( $self->{_encoding} // '' );
63             }
64              
65             # encoding( [$value] )
66             # Gets or sets the encoding token. Validates against VALID_ENCODINGS.
67             sub encoding
68             {
69 53     53 1 117 my $self = shift( @_ );
70 53 50       170 if( @_ )
71             {
72 53   50     415 my $enc = lc( shift( @_ ) // '' );
73 53 100       627 unless( exists( $VALID_ENCODINGS{ $enc } ) )
74             {
75 1         20 return( $self->error( "Invalid Content-Transfer-Encoding '$enc'; must be one of: " .
76             join( ', ', sort( keys( %VALID_ENCODINGS ) ) ) ) );
77             }
78 52         253 $self->{_encoding} = $enc;
79 52         287 return( $self );
80             }
81 0         0 return( $self->{_encoding} );
82             }
83              
84             # is_binary()
85             # Returns true if the encoding is 'binary' (never valid for mail text parts).
86 52 50 50 52 1 635 sub is_binary { return( ( shift->{_encoding} // '' ) eq 'binary' ? 1 : 0 ); }
87              
88             # is_encoded()
89             # Returns true if the encoding is base64 or quoted-printable.
90             sub is_encoded
91             {
92 0   0 0 1   my $enc = shift->{_encoding} // '';
93 0 0 0       return( ( $enc eq 'base64' || $enc eq 'quoted-printable' ) ? 1 : 0 );
94             }
95              
96             # value() - alias for as_string
97 0     0 1   sub value { return( shift->as_string ); }
98              
99             # NOTE: STORABLE support
100 0     0 0   sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
101              
102 0     0 0   sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
103              
104             1;
105             # NOTE: POD
106             __END__
107              
108             =encoding utf-8
109              
110             =head1 NAME
111              
112             Mail::Make::Headers::ContentTransferEncoding - Typed Content-Transfer-Encoding Header for Mail::Make
113              
114             =head1 SYNOPSIS
115              
116             use Mail::Make::Headers::ContentTransferEncoding;
117              
118             my $cte = Mail::Make::Headers::ContentTransferEncoding->new( 'quoted-printable' ) ||
119             die( Mail::Make::Headers::ContentTransferEncoding->error );
120             print $cte->as_string;
121             # quoted-printable
122              
123             =head1 VERSION
124              
125             v0.2.0
126              
127             =head1 DESCRIPTION
128              
129             A typed, validating object for the C<Content-Transfer-Encoding> header field.
130              
131             Accepts only the RFC 2045-defined encoding tokens: C<7bit>, C<8bit>, C<binary>, C<base64>, and C<quoted-printable>.
132              
133             =head1 CONSTRUCTOR
134              
135             =head2 new( $encoding )
136              
137             Creates a new object with the given encoding token. Returns an error if the token is not one of the RFC 2045-defined values.
138              
139             =head1 METHODS
140              
141             =head2 as_string
142              
143             Returns the encoding token string.
144              
145             =head2 encoding( [$value] )
146              
147             Gets or sets the encoding token. Validates against allowed values.
148              
149             =head2 is_binary
150              
151             Returns true if the encoding is C<binary>.
152              
153             =head2 is_encoded
154              
155             Returns true if the encoding is C<base64> or C<quoted-printable>.
156              
157             =head2 value
158              
159             Alias for C<as_string>.
160              
161             =head1 AUTHOR
162              
163             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
164              
165             =head1 SEE ALSO
166              
167             RFC 2045 section 6
168              
169             L<Mail::Make::Headers::ContentType>, L<Mail::Make::Headers>, L<Mail::Make>
170              
171             =head1 COPYRIGHT & LICENSE
172              
173             Copyright(c) 2026 DEGUEST Pte. Ltd.
174              
175             All rights reserved.
176              
177             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
178              
179             =cut