File Coverage

blib/lib/Mail/Box/FastScalar.pm
Criterion Covered Total %
statement 15 115 13.0
branch 0 32 0.0
condition 0 14 0.0
subroutine 5 39 12.8
pod 0 23 0.0
total 20 223 8.9


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Mail-Message version 4.04.
2             # The POD got stripped from this file by OODoc version 3.06.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2001-2026 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11              
12             package Mail::Box::FastScalar;{
13             our $VERSION = '4.04';
14             }
15              
16              
17 37     37   358 use strict;
  37         488  
  37         1662  
18 37     37   366 use warnings;
  37         145  
  37         2180  
19 37     37   20985 use integer;
  37         1355  
  37         255  
20              
21 37     37   1925 use Log::Report 'mail-message', import => [ qw// ];
  37         343  
  37         302  
22              
23 37     37   6051 use Scalar::Util qw/blessed/;
  37         338  
  37         69112  
24              
25             #--------------------
26              
27             sub new(;$)
28 0     0 0   { my ($class, $ref) = @_;
29 0           (bless +{ }, $class)->open($ref);
30             }
31              
32       0 0   sub autoflush() {}
33       0 0   sub binmode() {}
34 0     0 0   sub clearerr { 0 }
35       0 0   sub flush() {}
36             sub sync() { 0 }
37 0     0 0   sub opened() { $_[0]->{ref} }
38              
39             sub open($)
40 0     0 0   { my $self = $_[0];
41 0   0       my $ref = $self->{ref} = $_[1] // \(my $tmp);
42 0   0       $$ref //= '';
43 0           $self->{pos} = 0;
44 0           $self;
45             }
46              
47 0     0 0   sub close() { undef $_[0]->{ref} }
48              
49             sub eof()
50 0     0 0   { my $self = $_[0];
51 0           $self->{pos} >= length ${$self->{ref}};
  0            
52             }
53              
54             sub getc()
55 0     0 0   { my $self = $_[0];
56 0           substr ${$self->{ref}}, $self->{pos}++, 1;
  0            
57             }
58              
59             sub print
60 0     0 0   { my $self = shift;
61 0           my $pos = $self->{pos};
62 0           my $ref = $self->{ref};
63 0           my $len = length $$ref;
64              
65 0 0         if ($pos >= $len)
66 0           { $$ref .= $_ for @_;
67 0           $self->{pos} = length $$ref;
68             }
69             else
70 0 0         { my $buf = $#_ ? join('', @_) : $_[0];
71 0           $len = length $buf;
72 0           substr($$ref, $pos, $len) = $buf;
73 0           $self->{pos} = $pos + $len;
74             }
75              
76 0           1;
77             }
78              
79             sub read($$;$)
80 0     0 0   { my $self = $_[0];
81 0           my $buf = substr ${$self->{ref}}, $self->{pos}, $_[2];
  0            
82 0           $self->{pos} += $_[2];
83              
84 0 0         ($_[3] ? substr($_[1], $_[3]) : $_[1]) = $buf;
85 0           length $buf;
86             }
87              
88 0     0 0   sub sysread($$;$) { shift->read(@_) }
89              
90             sub seek($$)
91 0     0 0   { my ($self, $delta, $whence) = @_;
92 0           my $len = length ${$self->{ref}};
  0            
93              
94 0 0         if ($whence == 0) { $self->{pos} = $delta }
  0 0          
    0          
95 0           elsif ($whence == 1) { $self->{pos} += $delta }
96 0           elsif ($whence == 2) { $self->{pos} = $len + $delta }
97 0           else { return }
98              
99 0 0         if($self->{pos} > $len) { $self->{pos} = $len }
  0 0          
100 0           elsif($self->{pos} < 0) { $self->{pos} = 0 }
101              
102 0           1;
103             }
104              
105 0     0 0   sub sysseek($$) { $_[0]->seek($_[1], $_[2]) }
106 0     0 0   sub setpos($) { $_[0]->seek($_[1], 0) }
107 0     0 0   sub sref() { $_[0]->{ref} }
108 0     0 0   sub getpos() { $_[0]->{pos} }
109 0     0 0   sub tell() { $_[0]->{pos} }
110              
111             sub write($$;$)
112 0     0 0   { my $self = $_[0];
113 0           my $pos = $self->{pos};
114 0           my $ref = $self->{ref};
115 0           my $len = length $$ref;
116              
117 0 0         if($pos >= $len)
118 0   0       { $$ref .= substr($_[1], $_[3] || 0, $_[2]);
119 0           $self->{pos} = length $$ref;
120 0           $len = $self->{pos} - $len;
121             }
122             else
123 0   0       { my $buf = substr($_[1], $_[3] || 0, $_[2]);
124 0           $len = length $buf;
125 0           substr($$ref, $pos, $len) = $buf;
126 0           $self->{pos} = $pos + $len;
127             }
128              
129 0           $len;
130             }
131              
132 0     0 0   sub syswrite($;$$) { shift->write(@_) }
133              
134             sub getline()
135 0     0 0   { my $self = shift;
136 0           my $ref = $self->{ref};
137 0           my $pos = $self->{pos};
138              
139 0           my $idx;
140 0 0 0       if( !defined $/ || ($idx = index($$ref, $/, $pos)) == -1)
141 0 0         { return if $pos >= length $$ref;
142 0           $self->{pos} = length $$ref;
143 0           return substr $$ref, $pos;
144             }
145              
146 0           substr $$ref, $pos, ($self->{pos} = $idx + length $/) - $pos;
147             }
148              
149             sub getlines()
150 0     0 0   { my $self = $_[0];
151 0           my $ref = $self->{ref};
152 0           my $pos = $self->{pos};
153              
154 0           my @lines;
155 0 0         if(defined $/)
156 0           { my $idx;
157 0           my $sep_length = length $/;
158 0           while(($idx = index($$ref, $/, $pos)) != -1)
159 0           { push @lines, substr($$ref, $pos, $idx + $sep_length - $pos);
160 0           $pos = $idx + $sep_length;
161             }
162             }
163 0           my $r = substr $$ref, $pos;
164 0 0         push @lines, $r if length $r > 0;
165              
166 0           $self->{pos} = length $$ref;
167 0 0         wantarray ? @lines : \@lines;
168             }
169              
170             # Call OO, because this module might be extended
171 0 0 0 0     sub TIEHANDLE { blessed $_[1] && $_[1]->isa(__PACKAGE__) ? $_[1] : shift->new(@_) }
172 0     0     sub GETC { shift->getc(@_) }
173 0     0     sub PRINT { shift->print(@_) }
174 0     0     sub PRINTF { shift->print(sprintf shift, @_) }
175 0     0     sub READ { shift->read(@_) }
176 0 0   0     sub READLINE { wantarray ? shift->getlines(@_) : shift->getline(@_) }
177 0     0     sub WRITE { shift->write(@_) }
178 0     0     sub CLOSE { shift->close(@_) }
179 0     0     sub SEEK { shift->seek(@_) }
180 0     0     sub TELL { shift->tell(@_) }
181 0     0     sub EOF { shift->eof(@_) }
182              
183             1;