File Coverage

lib/Mail/Make/Body/InCore.pm
Criterion Covered Total %
statement 53 55 96.3
branch 11 16 68.7
condition n/a
subroutine 15 15 100.0
pod 7 7 100.0
total 86 93 92.4


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## MIME Email Builder - ~/lib/Mail/Make/Body/InCore.pm
3             ## Version v0.1.1
4             ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5             ## Author: Jacques Deguest <jack@deguest.jp>
6             ## Created 2026/03/02
7             ## Modified 2026/03/07
8             ## All rights reserved
9             ##
10             ##
11             ## This program is free software; you can redistribute it and/or modify it
12             ## under the same terms as Perl itself.
13             ##----------------------------------------------------------------------------
14             # NOTE: Mail::Make::Body::InCore package
15             # Body held entirely in memory as a scalar.
16             package Mail::Make::Body::InCore;
17             BEGIN
18             {
19 9     9   2999 use strict;
  9         17  
  9         356  
20 9     9   46 use warnings;
  9         16  
  9         585  
21 9     9   66 warnings::register_categories( 'Mail::Make' );
22 9     9   45 use parent qw( Mail::Make::Body );
  9         36  
  9         70  
23 9     9   744 use vars qw( $VERSION $EXCEPTION_CLASS );
  9         33  
  9         681  
24 9         14 our $EXCEPTION_CLASS = 'Mail::Make::Exception';
25 9         233 our $VERSION = 'v0.1.1';
26             }
27              
28 9     9   42 use strict;
  9         14  
  9         225  
29 9     9   46 use warnings;
  9         74  
  9         2737  
30              
31             sub init
32             {
33 113     113 1 372567 my $self = shift( @_ );
34 113         371 my $data = shift( @_ );
35 113         999 $self->{_data} = '';
36 113         418 $self->{_exception_class} = $EXCEPTION_CLASS;
37 113 50       538 $self->SUPER::init( @_ ) || return( $self->pass_error );
38 113 100       7670 if( defined( $data ) )
39             {
40             # Accept scalar reference or plain scalar
41 112 100       539 if( $self->_is_scalar( $data ) )
    100          
42             {
43 1         15 $self->{_data} = $$data;
44             }
45             elsif( !ref( $data ) )
46             {
47 110         1380 $self->{_data} = $data;
48             }
49             else
50             {
51 1         17 return( $self->error( "Mail::Make::Body::InCore->new requires a scalar or scalar reference, got: " . $self->_str_val( $data ) ) );
52             }
53             }
54 112         277 return( $self );
55             }
56              
57             sub as_string
58             {
59 44     44 1 1778 my $self = shift( @_ );
60 44         330 return( \$self->{_data} );
61             }
62              
63 1     1 1 9 sub is_in_core { return(1); }
64              
65             sub length
66             {
67 2     2 1 12 my $self = shift( @_ );
68 9     9   80 use bytes;
  9         15  
  9         74  
69 2         6 return( CORE::length( $self->{_data} ) );
70             }
71              
72             # Returns an in-memory filehandle opened for reading
73             sub open
74             {
75 46     46 1 185 my $self = shift( @_ );
76             # Copy to avoid closing over a reference to internal data
77 46         158 my $data = $self->{_data};
78             # If the scalar has the UTF-8 flag set (wide characters), encode it to a byte string
79             # before opening the in-memory filehandle, otherwise Perl will refuse with
80             # "Strings with code points over 0xFF may not be mapped into in-memory file handles".
81 46 50       191 utf8::encode( $data ) if( utf8::is_utf8( $data ) );
82 46 50       728 CORE::open( my $fh, '<', \$data ) ||
83             return( $self->error( "Cannot open in-core body for reading: $!" ) );
84 46         306 return( $fh );
85             }
86              
87             sub purge
88             {
89 2     2 1 10 my $self = shift( @_ );
90 2         6 $self->{_data} = '';
91 2         4 return( $self );
92             }
93              
94             # Allow direct assignment of data after construction
95             sub set
96             {
97 1     1 1 9 my $self = shift( @_ );
98 1         1 my $data = shift( @_ );
99 1 50       3 if( $self->_is_scalar( $data ) )
    50          
100             {
101 0         0 $self->{_data} = $$data;
102             }
103             elsif( !ref( $data ) )
104             {
105 1         8 $self->{_data} = $data;
106             }
107             else
108             {
109 0         0 return( $self->error( "Data must be a plain scalar or scalar reference." ) );
110             }
111 1         2 return( $self );
112             }
113              
114             1;
115             # NOTE: POD
116             __END__
117              
118             =encoding utf-8
119              
120             =head1 NAME
121              
122             Mail::Make::Body::InCore - In-Memory Body for Mail::Make
123              
124             =head1 SYNOPSIS
125              
126             use Mail::Make::Body::InCore;
127             my $body = Mail::Make::Body::InCore->new( "Hello, World!\n" ) ||
128             die( Mail::Make::Body::InCore->error );
129             my $fh = $body->open || die( $body->error );
130              
131             =head1 VERSION
132              
133             v0.1.1
134              
135             =head1 DESCRIPTION
136              
137             Holds mail body content in memory as a plain Perl scalar. Accepts a plain scalar or a scalar reference at construction time.
138              
139             =head1 CONSTRUCTOR
140              
141             =head2 new( $data )
142              
143             Accepts either a plain scalar or a scalar reference. Returns the object, or sets an error and returns C<undef>.
144              
145             =head1 METHODS
146              
147             =head2 as_string
148              
149             Returns a scalar reference to the internal data.
150              
151             =head2 is_in_core
152              
153             Returns true (1).
154              
155             =head2 length
156              
157             Returns the byte length of the stored data.
158              
159             =head2 open
160              
161             Returns a read-only in-memory filehandle.
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::Body::File>, L<Mail::Make::Body>, 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