| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | ## MIME Email Builder - ~/lib/Mail/Make/Body/File.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 | # NOTE: Mail::Make::Body::File package | ||||||||||||||||||||||||||||
| 14 | # Body stored on disk; only the file path is kept in memory. | ||||||||||||||||||||||||||||
| 15 | package Mail::Make::Body::File; | ||||||||||||||||||||||||||||
| 16 | BEGIN | ||||||||||||||||||||||||||||
| 17 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 8 | warnings::register_categories( 'Mail::Make' ); | |||||||||||||||||||||||||||
| 21 | 8 | ▶ | use parent qw( Mail::Make::Body ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 8 | ▶ | use vars qw( $VERSION $EXCEPTION_CLASS ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 8 | our $EXCEPTION_CLASS = 'Mail::Make::Exception'; | |||||||||||||||||||||||||||
| 24 | 8 | our $VERSION = 'v0.1.0'; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub init | ||||||||||||||||||||||||||||
| 31 | 49 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 49 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 33 | 49 | ▶ | my $path = shift( @_ ) || | ||||||||||||||||||||||||||
|
Condition50%1/2
shift @_ || (return $self->error('Mail::Make::Body::File->new requires a file path'))
MC/DC0%0/1
shift @_ || (return $self->error('Mail::Make::Body::File->new requires a file path'))
shift @_
Truth table50%1/2
A
A= shift @_
| |||||||||||||||||||||||||||||
| 34 | return( $self->error( "Mail::Make::Body::File->new requires a file path" ) ); | ||||||||||||||||||||||||||||
| 35 | 49 | $path = $self->new_file( $path ); | |||||||||||||||||||||||||||
| 36 | 49 | ▶ | unless( $path->exists && $path->can_read ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$path->exists and $path->can_read
MC/DC50%1/2
$path->exists and $path->can_read
$path->exists
$path->can_read
Truth table66%2/3
A and B
A= $path->existsB= $path->can_read
| |||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||
| 38 | 2 | return( $self->error( "File does not exist or is not readable: $path" ) ); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | # new_file() is inherited from Module::Generic and returns a new Module::Generic::File object | ||||||||||||||||||||||||||||
| 41 | 47 | $self->{_path} = $self->new_file( $path ); | |||||||||||||||||||||||||||
| 42 | 47 | $self->{_exception_class} = $EXCEPTION_CLASS; | |||||||||||||||||||||||||||
| 43 | 47 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 47 | return( $self ); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub as_string | ||||||||||||||||||||||||||||
| 48 | 14 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 14 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 50 | 14 | ▶ | my $fh = $self->open || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->open || (return $self->pass_error)
MC/DC0%0/1
$self->open || (return $self->pass_error)
$self->open
Truth table50%1/2
A
A= $self->open
| |||||||||||||||||||||||||||||
| 51 | # If we passed the 'open' statement, then we have a file, so we are safe. | ||||||||||||||||||||||||||||
| 52 | 14 | my $path = $self->{_path}; | |||||||||||||||||||||||||||
| 53 | 14 | my $data = ''; | |||||||||||||||||||||||||||
| 54 | 14 | my( $buf, $n ); | |||||||||||||||||||||||||||
| 55 | 14 | ▶ | while( $n = $fh->read( $buf, 65536 ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||
| 57 | 14 | $data .= $buf; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 14 | ▶ | return( $self->error( "Error reading file '$path': $!" ) ) if( !defined( $n ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 14 | $fh->close; | |||||||||||||||||||||||||||
| 61 | 14 | return( \$data ); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 13 | ▶ | sub is_on_file { return(1); } | ||||||||||||||||||||||||||
|
Subroutine
is_on_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub length | ||||||||||||||||||||||||||||
| 67 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
length: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 1 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 69 | my $path = $self->{_path} || | ||||||||||||||||||||||||||||
| 70 | 1 | ▶ | return( $self->error( "No file path has been set yet." ) ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'_path'} || (return $self->error('No file path has been set yet.'))
MC/DC0%0/1
$self->{'_path'} || (return $self->error('No file path has been set yet.'))
$self->{'_path'}
Truth table50%1/2
A
A= $self->{'_path'}
| |||||||||||||||||||||||||||||
| 71 | 1 | return( $path->length ); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # Returns a binary filehandle opened for reading | ||||||||||||||||||||||||||||
| 75 | sub open | ||||||||||||||||||||||||||||
| 76 | 26 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
open: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 26 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 78 | 26 | my $path = $self->{_path}; | |||||||||||||||||||||||||||
| 79 | 26 | ▶ | return( $self->error( "No file path set." ) ) if( !defined( $path ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 26 | ▶ | my $fh = $path->open( '<' ) || | ||||||||||||||||||||||||||
|
Condition50%1/2
$path->open('<') || (return $self->error("Cannot open file '${path}' for reading: ", $path->error))
MC/DC0%0/1
$path->open('<') || (return $self->error("Cannot open file '${path}' for reading: ", $path->error))
$path->open('<')
Truth table50%1/2
A
A= $path->open('<')
| |||||||||||||||||||||||||||||
| 81 | return( $self->error( "Cannot open file '$path' for reading: ", $path->error ) ); | ||||||||||||||||||||||||||||
| 82 | 26 | $fh->binmode( ':raw' ); | |||||||||||||||||||||||||||
| 83 | 26 | return( $fh ); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub path | ||||||||||||||||||||||||||||
| 87 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
path: called CC=6 SCAR=20.2
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 2 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 89 | 2 | ▶ | if( @_ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||
| 91 | 1 | ▶ | my $path = shift( @_ ) || | ||||||||||||||||||||||||||
|
Condition50%1/2
shift @_ || (return $self->error('No file was provided.'))
MC/DC0%0/1
shift @_ || (return $self->error('No file was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
| |||||||||||||||||||||||||||||
| 92 | return( $self->error( "No file was provided." ) ); | ||||||||||||||||||||||||||||
| 93 | 1 | ▶ | $path = $self->new_file( $path ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->new_file($path) || (return $self->pass_error)
MC/DC0%0/1
$self->new_file($path) || (return $self->pass_error)
$self->new_file($path)
Truth table50%1/2
A
A= $self->new_file($path)
| |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | unless( $path->exists && $path->can_read ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$path->exists and $path->can_read
MC/DC0%0/2
$path->exists and $path->can_read
$path->exists
$path->can_read
Truth table33%1/3
A and B
A= $path->existsB= $path->can_read
| |||||||||||||||||||||||||||||
| 95 | { | ||||||||||||||||||||||||||||
| 96 | 1 | return( $self->error( "File does not exist or is not readable: $path" ) ); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 0 | $self->{_path} = $path; | |||||||||||||||||||||||||||
| 99 | 0 | return( $self ); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 1 | return( $self->{_path} ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub purge | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
purge: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 107 | 0 | my $path = $self->{_path}; | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | if( defined( $path ) && -e $path ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $path and -e $path
MC/DC0%0/2
defined $path and -e $path
defined $path
-e $path
Truth table0%0/3
A and B
A= defined $pathB= -e $path
| |||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $path->remove || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | return( $self->error( "Cannot unlink '$path': ", $path->error ) ); | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 0 | $self->{_path} = undef; | |||||||||||||||||||||||||||
| 114 | 0 | return( $self ); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 8 | 1; | |||||||||||||||||||||||||||
| 118 | # NOTE: POD | ||||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||