1# Copyrights 2001-2021 by [Mark Overmeer <markov@cpan.org>].
2#  For other contributors see ChangeLog.
3# See the manual pages for details on the licensing terms.
4# Pod stripped from pm file by OODoc 2.02.
5# This code is part of distribution Mail-Message.  Meta-POD processed with
6# OODoc into POD and HTML manual-pages.  See README.md
7# Copyright Mark Overmeer.  Licensed under the same terms as Perl itself.
8
9package Mail::Message::Body::Lines;
10use vars '$VERSION';
11$VERSION = '3.011';
12
13use base 'Mail::Message::Body';
14
15use strict;
16use warnings;
17
18use Mail::Box::Parser;
19use IO::Lines;
20
21use Carp;
22
23
24sub _data_from_filename(@)
25{   my ($self, $filename) = @_;
26
27    local *IN;
28
29    unless(open IN, '<', $filename)
30    {   $self->log(ERROR =>
31             "Unable to read file $filename for message body lines: $!");
32        return;
33    }
34
35    $self->{MMBL_array} = [ <IN> ];
36
37    close IN;
38    $self;
39}
40
41sub _data_from_filehandle(@)
42{   my ($self, $fh) = @_;
43    $self->{MMBL_array} =
44       ref $fh eq 'Mail::Box::FastScalar' ? $fh->getlines : [ $fh->getlines ];
45    $self
46}
47
48sub _data_from_glob(@)
49{   my ($self, $fh) = @_;
50    $self->{MMBL_array} = [ <$fh> ];
51    $self;
52}
53
54sub _data_from_lines(@)
55{   my ($self, $lines)  = @_;
56    $lines = [ split /^/, $lines->[0] ]    # body passed in one string.
57        if @$lines==1;
58
59    $self->{MMBL_array} = $lines;
60    $self;
61}
62
63#------------------------------------------
64
65sub clone()
66{   my $self  = shift;
67    ref($self)->new(data => [ $self->lines ], based_on => $self);
68}
69
70#------------------------------------------
71
72sub nrLines() { scalar @{shift->{MMBL_array}} }
73
74#------------------------------------------
75# Optimized to be computed only once.
76
77sub size()
78{   my $self = shift;
79    return $self->{MMBL_size} if exists $self->{MMBL_size};
80
81    my $size = 0;
82    $size += length $_ foreach @{$self->{MMBL_array}};
83    $self->{MMBL_size} = $size;
84}
85
86#------------------------------------------
87
88sub string() { join '', @{shift->{MMBL_array}} }
89
90#------------------------------------------
91
92sub lines() { wantarray ? @{shift->{MMBL_array}} : shift->{MMBL_array} }
93
94#------------------------------------------
95
96sub file() { IO::Lines->new(shift->{MMBL_array}) }
97
98#------------------------------------------
99
100sub print(;$)
101{   my $self = shift;
102    my $fh   = shift || select;
103    if(ref $fh eq 'GLOB') { print $fh @{$self->{MMBL_array}}   }
104    else                  { $fh->print(@{$self->{MMBL_array}}) }
105    $self;
106}
107
108#------------------------------------------
109
110sub read($$;$@)
111{   my ($self, $parser, $head, $bodytype) = splice @_, 0, 4;
112    my ($begin, $end, $lines) = $parser->bodyAsList(@_);
113    $lines or return undef;
114
115    $self->fileLocation($begin, $end);
116    $self->{MMBL_array} = $lines;
117    $self;
118}
119
120#------------------------------------------
121
122sub endsOnNewline()
123{   my $last = shift->{MMBL_array}[-1];
124    !defined $last || $last =~ m/\n$/;
125}
126
127#------------------------------------------
128
1291;
130