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

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 25 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 125 CC 7 SCAR 16.3
CC
7
Cov
0%
path
12.0
init
6.0
as_string
6.0
CRAP 5.1
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 {
18 use strict;
19 use warnings;
20 warnings::register_categories( 'Mail::Make' );
21 use parent qw( Mail::Make::Body );
22 use vars qw( $VERSION $EXCEPTION_CLASS );
23 our $EXCEPTION_CLASS = 'Mail::Make::Exception';
24 our $VERSION = 'v0.1.0';
25 }
26  
27 use strict;
28 use warnings;
29  
30 sub init
31 {
32 my $self = shift( @_ );
33 my $path = shift( @_ ) ||
34 return( $self->error( "Mail::Make::Body::File->new requires a file path" ) );
35 $path = $self->new_file( $path );
36 unless( $path->exists && $path->can_read )
37 {
38 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 $self->{_path} = $self->new_file( $path );
42 $self->{_exception_class} = $EXCEPTION_CLASS;
43 $self->SUPER::init( @_ ) || return( $self->pass_error );
44 return( $self );
45 }
46  
47 sub as_string
48 {
49 my $self = shift( @_ );
50 my $fh = $self->open || return( $self->pass_error );
51 # If we passed the 'open' statement, then we have a file, so we are safe.
52 my $path = $self->{_path};
53 my $data = '';
54 my( $buf, $n );
55 while( $n = $fh->read( $buf, 65536 ) )
56 {
57 $data .= $buf;
58 }
59 return( $self->error( "Error reading file '$path': $!" ) ) if( !defined( $n ) );
60 $fh->close;
61 return( \$data );
62 }
63  
64 sub is_on_file { return(1); }
65  
66 sub length
67 {
68 my $self = shift( @_ );
69 my $path = $self->{_path} ||
70 return( $self->error( "No file path has been set yet." ) );
71 return( $path->length );
72 }
73  
74 # Returns a binary filehandle opened for reading
75 sub open
76 {
77 my $self = shift( @_ );
78 my $path = $self->{_path};
79 return( $self->error( "No file path set." ) ) if( !defined( $path ) );
80 my $fh = $path->open( '<' ) ||
81 return( $self->error( "Cannot open file '$path' for reading: ", $path->error ) );
82 $fh->binmode( ':raw' );
83 return( $fh );
84 }
85  
86 sub path
87 {
88 my $self = shift( @_ );
89 if( @_ )
90 {
91 my $path = shift( @_ ) ||
92 return( $self->error( "No file was provided." ) );
93 $path = $self->new_file( $path ) || return( $self->pass_error );
94 unless( $path->exists && $path->can_read )
95 {
96 return( $self->error( "File does not exist or is not readable: $path" ) );
97 }
98 $self->{_path} = $path;
99 return( $self );
100 }
101 return( $self->{_path} );
102 }
103  
104 sub purge
105 {
106 my $self = shift( @_ );
107 my $path = $self->{_path};
108 if( defined( $path ) && -e $path )
109 {
110 $path->remove ||
111 return( $self->error( "Cannot unlink '$path': ", $path->error ) );
112 }
113 $self->{_path} = undef;
114 return( $self );
115 }
116  
117 1;
118 # NOTE: POD
119 __END__