blib/lib/Mail/Make/Body/InCore.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 96 CC 7 SCAR 19.8
CC
7
Cov
0%
init
20.0
set
12.0
open
6.0
CRAP 7.2
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 use strict;
20 use warnings;
21 warnings::register_categories( 'Mail::Make' );
22 use parent qw( Mail::Make::Body );
23 use vars qw( $VERSION $EXCEPTION_CLASS );
24 our $EXCEPTION_CLASS = 'Mail::Make::Exception';
25 our $VERSION = 'v0.1.1';
26 }
27  
28 use strict;
29 use warnings;
30  
31 sub init
32 {
33 my $self = shift( @_ );
34 my $data = shift( @_ );
35 $self->{_data} = '';
36 $self->{_exception_class} = $EXCEPTION_CLASS;
37 $self->SUPER::init( @_ ) || return( $self->pass_error );
38 if( defined( $data ) )
39 {
40 # Accept scalar reference or plain scalar
41 if( $self->_is_scalar( $data ) )
42 {
43 $self->{_data} = $$data;
44 }
45 elsif( !ref( $data ) )
46 {
47 $self->{_data} = $data;
48 }
49 else
50 {
51 return( $self->error( "Mail::Make::Body::InCore->new requires a scalar or scalar reference, got: " . $self->_str_val( $data ) ) );
52 }
53 }
54 return( $self );
55 }
56  
57 sub as_string
58 {
59 my $self = shift( @_ );
60 return( \$self->{_data} );
61 }
62  
63 sub is_in_core { return(1); }
64  
65 sub length
66 {
67 my $self = shift( @_ );
68 use bytes;
69 return( CORE::length( $self->{_data} ) );
70 }
71  
72 # Returns an in-memory filehandle opened for reading
73 sub open
74 {
75 my $self = shift( @_ );
76 # Copy to avoid closing over a reference to internal data
77 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 utf8::encode( $data ) if( utf8::is_utf8( $data ) );
82 CORE::open( my $fh, '<', \$data ) ||
83 return( $self->error( "Cannot open in-core body for reading: $!" ) );
84 return( $fh );
85 }
86  
87 sub purge
88 {
89 my $self = shift( @_ );
90 $self->{_data} = '';
91 return( $self );
92 }
93  
94 # Allow direct assignment of data after construction
95 sub set
96 {
97 my $self = shift( @_ );
98 my $data = shift( @_ );
99 if( $self->_is_scalar( $data ) )
100 {
101 $self->{_data} = $$data;
102 }
103 elsif( !ref( $data ) )
104 {
105 $self->{_data} = $data;
106 }
107 else
108 {
109 return( $self->error( "Data must be a plain scalar or scalar reference." ) );
110 }
111 return( $self );
112 }
113  
114 1;
115 # NOTE: POD
116 __END__