| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||
| 2 | ## MIME Email Builder - ~/lib/Mail/Make/Body/InCore.pm | ||||||||||||||
| 3 | ## Version v0.1.2 | ||||||||||||||
| 4 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||||||||||||||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||||||
| 6 | ## Created 2026/03/02 | ||||||||||||||
| 7 | ## Modified 2026/05/11 | ||||||||||||||
| 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 | 9 | ▶ | { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 9 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 9 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 9 | warnings::register_categories( 'Mail::Make' ); | |||||||||||||
| 22 | 9 | ▶ | use parent qw( Mail::Make::Body ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 9 | ▶ | use vars qw( $VERSION $EXCEPTION_CLASS ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 9 | our $EXCEPTION_CLASS = 'Mail::Make::Exception'; | |||||||||||||
| 25 | 9 | our $VERSION = 'v0.1.2'; | |||||||||||||
| 26 | } | ||||||||||||||
| 27 | |||||||||||||||
| 28 | 9 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | 9 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | |||||||||||||||
| 31 | sub init | ||||||||||||||
| 32 | 156 | ▶ | { | ||||||||||||
|
Subroutine
init: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 33 | 156 | my $self = shift( @_ ); | |||||||||||||
| 34 | 156 | my $data = shift( @_ ); | |||||||||||||
| 35 | 156 | $self->{_data} = ''; | |||||||||||||
| 36 | 156 | $self->{_exception_class} = $EXCEPTION_CLASS; | |||||||||||||
| 37 | 156 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | 156 | ▶ | if( defined( $data ) ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | { | ||||||||||||||
| 40 | # Accept scalar reference or plain scalar | ||||||||||||||
| 41 | 155 | ▶ | if( $self->_is_scalar( $data ) ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 42 | { | ||||||||||||||
| 43 | 1 | $self->{_data} = $$data; | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | elsif( !ref( $data ) ) | ||||||||||||||
| 46 | { | ||||||||||||||
| 47 | 153 | $self->{_data} = $data; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | else | ||||||||||||||
| 50 | { | ||||||||||||||
| 51 | 1 | return( $self->error( "Mail::Make::Body::InCore->new requires a scalar or scalar reference, got: " . $self->_str_val( $data ) ) ); | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | } | ||||||||||||||
| 54 | 155 | return( $self ); | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | sub as_string | ||||||||||||||
| 58 | 49 | ▶ | { | ||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 59 | 49 | my $self = shift( @_ ); | |||||||||||||
| 60 | 49 | return( \$self->{_data} ); | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 1 | ▶ | sub is_in_core { return(1); } | ||||||||||||
|
Subroutine
is_in_core: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 64 | |||||||||||||||
| 65 | sub length | ||||||||||||||
| 66 | 2 | ▶ | { | ||||||||||||
|
Subroutine
length: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 67 | 2 | my $self = shift( @_ ); | |||||||||||||
| 68 | 9 | ▶ | use bytes; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 69 | 2 | return( CORE::length( $self->{_data} ) ); | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | # Returns an in-memory filehandle opened for reading | ||||||||||||||
| 73 | sub open | ||||||||||||||
| 74 | 46 | ▶ | { | ||||||||||||
|
Subroutine
open: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||
| 75 | 46 | my $self = shift( @_ ); | |||||||||||||
| 76 | # Copy to avoid closing over a reference to internal data | ||||||||||||||
| 77 | 46 | 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 | ▶ | utf8::encode( $data ) if( utf8::is_utf8( $data ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 46 | ▶ | CORE::open( my $fh, '<', \$data ) || | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | return( $self->error( "Cannot open in-core body for reading: $!" ) ); | ||||||||||||||
| 84 | 46 | return( $fh ); | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | sub purge | ||||||||||||||
| 88 | 2 | ▶ | { | ||||||||||||
|
Subroutine
purge: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 89 | 2 | my $self = shift( @_ ); | |||||||||||||
| 90 | 2 | $self->{_data} = ''; | |||||||||||||
| 91 | 2 | return( $self ); | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | # Allow direct assignment of data after construction | ||||||||||||||
| 95 | sub set | ||||||||||||||
| 96 | 1 | ▶ | { | ||||||||||||
|
Subroutine
set: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||
| 97 | 1 | my $self = shift( @_ ); | |||||||||||||
| 98 | 1 | my $data = shift( @_ ); | |||||||||||||
| 99 | 1 | ▶ | if( $self->_is_scalar( $data ) ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 100 | { | ||||||||||||||
| 101 | 0 | $self->{_data} = $$data; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | elsif( !ref( $data ) ) | ||||||||||||||
| 104 | { | ||||||||||||||
| 105 | 1 | $self->{_data} = $data; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | else | ||||||||||||||
| 108 | { | ||||||||||||||
| 109 | 0 | return( $self->error( "Data must be a plain scalar or scalar reference." ) ); | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | 1 | return( $self ); | |||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 9 | 1; | |||||||||||||
| 115 | # NOTE: POD | ||||||||||||||
| 116 | __END__ | ||||||||||||||