-
Notifications
You must be signed in to change notification settings - Fork 26
/
STDIN.pm
137 lines (95 loc) · 2.82 KB
/
STDIN.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package Sisimai::Mail::STDIN;
use feature ':5.10';
use strict;
use warnings;
use IO::Handle;
use Class::Accessor::Lite (
'new' => 0,
'ro' => [
'path', # [String] Fixed string "<STDIN>"
'name', # [String] File name of the mbox
'size', # [Integer] File size of the mbox
],
'rw' => [
'offset', # [Integer] Offset position for seeking
'handle', # [IO::File] File handle
]
);
sub new {
# Constructor of Sisimai::Mail::STDIN
# @return [Sisimai::Mail::STDIN] Object
my $class = shift;
my $param = {
'path' => '<STDIN>',
'name' => '<STDIN>',
'size' => undef,
'offset' => 0,
'handle' => IO::Handle->new->fdopen(fileno(STDIN), 'r'),
};
return bless($param, __PACKAGE__);
}
sub read {
# Mbox reader, works as a iterator.
# @return [String] Contents of mbox
my $self = shift;
return undef unless -T $self->{'handle'};
my $readhandle = $self->{'handle'};
my $readbuffer = '';
eval {
$readhandle = $self->{'handle'}->fdopen(fileno(STDIN), 'r') unless eof $readhandle;
while( my $r = <$readhandle> ) {
# Read an email from the mailbox file
last if( $readbuffer && substr($r, 0, 5) eq 'From ' );
$readbuffer .= $r;
}
};
return $readbuffer;
}
1;
__END__
=encoding utf-8
=head1 NAME
Sisimai::Mail::STDIN - Mailbox reader
=head1 SYNOPSIS
use Sisimai::Mail::STDIN;
my $mailbox = Sisimai::Mail::STDIN->new();
while( my $r = $mailbox->read ) {
print $r; # print data read from STDIN
}
$mailbox->close;
=head1 DESCRIPTION
Sisimai::Mail::STDIN read email data from STDIN.
=head1 CLASS METHODS
=head2 C<B<new()>>
C<new()> is a constructor of Sisimai::Mail::STDIN
my $mailbox = Sisimai::Mail::STDIN->new();
=head1 INSTANCE METHODS
=head2 C<B<path()>>
C<path()> returns "<STDIN>"
print $mailbox->path; # "<STDIN>"
=head2 C<B<name()>>
C<name()> returns "<STDIN>"
print $mailbox->name; # "<STDIN>"
=head2 C<B<size()>>
C<size()> returns "undef"
print $mailbox->size; # undef
=head2 C<B<offset()>>
C<offset()> returns offset position for seeking the mbox. The value of "offset"
is bytes which have already read.
print $mailbox->offset; # 0
=head2 C<B<handle()>>
C<handle()> returns file handle object (IO::Handle) of the mbox.
$mailbox->handle->close;
=head2 C<B<read()>>
C<read()> works as a iterator for reading each email in the mbox.
my $mailbox = Sisimai::Mail->new();
while( my $r = $mailbox->read ) {
print $r; # print data read from STDIN
}
=head1 AUTHOR
azumakuniyuki
=head1 COPYRIGHT
Copyright (C) 2014-2016,2018-2020 azumakuniyuki, All rights reserved.
=head1 LICENSE
This software is distributed under The BSD 2-Clause License.
=cut