File Coverage

lib/Mail/Make/Body.pm
Criterion Covered Total %
statement 26 36 72.2
branch n/a
condition 0 2 0.0
subroutine 10 17 58.8
pod 7 9 77.7
total 43 64 67.1


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## MIME Email Builder - ~/lib/Mail/Make/Body.pm
3             ## Version v0.1.0
4             ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5             ## Author: Jacques Deguest <jack@deguest.jp>
6             ## Created 2026/03/02
7             ## Modified 2026/03/02
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::Body;
14             BEGIN
15             {
16 9     9   583186 use strict;
  9         18  
  9         360  
17 9     9   44 use warnings;
  9         28  
  9         608  
18 9     9   3508 warnings::register_categories( 'Mail::Make' );
19 9     9   483 use parent qw( Module::Generic );
  9         398  
  9         66  
20 9     9   273106 use vars qw( $VERSION $EXCEPTION_CLASS );
  9         20  
  9         680  
21 9     9   3335 use Mail::Make::Exception;
  9         22  
  9         92  
22 9         16 our $EXCEPTION_CLASS = 'Mail::Make::Exception';
23 9         303 our $VERSION = 'v0.1.0';
24             };
25              
26 9     9   57 use strict;
  9         19  
  9         197  
27 9     9   32 use warnings;
  9         17  
  9         2277  
28              
29             # Returns the body content as a scalar ref.
30             # Must be overridden by subclasses.
31             sub as_string
32             {
33 0     0 1 0 my $self = shift( @_ );
34 0         0 return( $self->error( ref( $self ) . "::as_string() is not implemented." ) );
35             }
36              
37 0     0 1 0 sub data { return( shift->as_string( @_ ) ); }
38              
39             # Returns the byte length of the body content.
40             sub length
41             {
42 0     0 1 0 my $self = shift( @_ );
43 0   0     0 my $string = $self->as_string || return( $self->pass_error );
44 0         0 return( CORE::length( $$string ) );
45             }
46              
47             # Returns true if the body is stored on disk (Body::File), false otherwise.
48 39     39 1 127 sub is_on_file { return(0); }
49              
50             # Returns true if the body is stored in memory (Body::InCore), false otherwise.
51 1     1 1 6394 sub is_in_core { return(0); }
52              
53             # path() - only meaningful for Body::File; returns undef here.
54 0     0 1   sub path { return; }
55              
56             # Empties / releases the body content. Subclasses override.
57 0     0 1   sub purge { return( shift ); }
58              
59             # NOTE: STORABLE support
60 0     0 0   sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
61              
62 0     0 0   sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
63              
64             1;
65             # NOTE: POD
66             __END__
67              
68             =encoding utf-8
69              
70             =head1 NAME
71              
72             Mail::Make::Body - MIME Body Base Class for Mail::Make
73              
74             =head1 SYNOPSIS
75              
76             use Mail::Make::Body;
77              
78             # In-memory body
79             my $b = Mail::Make::Body::InCore->new( "Hello, world!" ) ||
80             die( Mail::Make::Body::InCore->error );
81             my $ref = $b->as_string; # scalar ref
82             print $$ref;
83              
84             # File-backed body
85             my $f = Mail::Make::Body::File->new( '/path/to/logo.png' ) ||
86             die( Mail::Make::Body::File->error );
87             my $ref = $f->as_string; # reads entire file into memory
88              
89             =head1 VERSION
90              
91             v0.1.0
92              
93             =head1 DESCRIPTION
94              
95             C<Mail::Make::Body> is the abstract base class for MIME body objects used by L<Mail::Make::Entity>. Two concrete subclasses are provided in the same file:
96              
97             =over 4
98              
99             =item L</Mail::Make::Body::InCore>
100              
101             Holds the body content in memory as a scalar. Suitable for text parts and small attachments assembled at runtime.
102              
103             =item L</Mail::Make::Body::File>
104              
105             Holds a path to a file on disk. Content is read on demand by L</as_string>. The file is validated for existence and readability at construction time.
106              
107             =back
108              
109             =head1 METHODS (Mail::Make::Body)
110              
111             =head2 as_string
112              
113             Returns a scalar reference to the body content. Must be overridden by subclasses.
114              
115             =head2 data
116              
117             Alias for L</as_string>.
118              
119             =head2 is_in_core
120              
121             Returns 1 if this is a L<Mail::Make::Body::InCore> object, 0 otherwise.
122              
123             =head2 is_on_file
124              
125             Returns 1 if this is a L<Mail::Make::Body::File> object, 0 otherwise.
126              
127             =head2 length
128              
129             Returns the byte length of the body content.
130              
131             =head2 path
132              
133             Returns C<undef> for the base class. Overridden by L<Mail::Make::Body::File>.
134              
135             =head2 purge
136              
137             Releases the body content. Subclasses override.
138              
139             =head1 METHODS (Mail::Make::Body::InCore)
140              
141             =head2 new( [ $data ] )
142              
143             Creates a new in-memory body. C<$data> may be a plain scalar or a scalar reference.
144              
145             =head2 set( $data )
146              
147             Replaces the stored data after construction.
148              
149             =head1 METHODS (Mail::Make::Body::File)
150              
151             =head2 new( [ $path ] )
152              
153             Creates a new file-backed body. Validates that C<$path> exists and is readable.
154              
155             =head2 path( [ $path ] )
156              
157             Sets or gets the file path. Validates existence and readability on assignment.
158              
159             =head2 purge
160              
161             Deletes the file from disk and clears the path.
162              
163             =head1 AUTHOR
164              
165             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
166              
167             =head1 SEE ALSO
168              
169             L<Mail::Make>, L<Mail::Make::Entity>
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