如何按日期对 mbox 文件进行排序?
我想知道如何修改下面的代码以在多行末尾去除 =20
字符,主要是按时间顺序对消息进行排序,从第一次接收或发送到最后一次。 我不确定这是否是 Perl 内部例程。
#!/usr/bin/perl
use warnings;
use strict;
use Mail::Box::Manager;
my $file = shift || $ENV{MAIL};
my $mgr = Mail::Box::Manager->new(
access => 'r',
);
my $folder = $mgr->open( folder => $file )
or die "$file: Unable to open: $!\n";
for my $msg ($folder->messages)
{
my $to = join( ', ', map { $_->format } $msg->to );
my $from = join( ', ', map { $_->format } $msg->from );
my $date = localtime( $msg->timestamp );
my $subject = $msg->subject;
my $body = $msg->body;
# Strip all quoted text
$body =~ s/^>.*$//msg;
print <<"";
From: $from
To: $to
Date: $date
$body
}
当尝试运行此程序时,我收到以下错误:
“my”变量 $msg 掩盖了 x.pl 第 16 行相同范围内的早期声明。 x.pl 第 15 行的语法错误,靠近“) )” x.pl 第 31 行“}”附近的语法错误 (可能是从第 25 行开始的失控多行 << 字符串) x.pl 的执行由于编译错误而中止。
我不确定为什么,因为语法看起来不错。
I would like to know how to modify the below code to strip =20
characters at the end of many lines, and mainly to sort the messages chronologically from first received or sent to last. I am not sure if this would be an internal Perl routine or not.
#!/usr/bin/perl
use warnings;
use strict;
use Mail::Box::Manager;
my $file = shift || $ENV{MAIL};
my $mgr = Mail::Box::Manager->new(
access => 'r',
);
my $folder = $mgr->open( folder => $file )
or die "$file: Unable to open: $!\n";
for my $msg ($folder->messages)
{
my $to = join( ', ', map { $_->format } $msg->to );
my $from = join( ', ', map { $_->format } $msg->from );
my $date = localtime( $msg->timestamp );
my $subject = $msg->subject;
my $body = $msg->body;
# Strip all quoted text
$body =~ s/^>.*$//msg;
print <<"";
From: $from
To: $to
Date: $date
$body
}
When trying to run this I get the following errors:
"my" variable $msg masks earlier declaration in same scope at x.pl line 16.
syntax error at x.pl line 15, near ") ) "
syntax error at x.pl line 31, near "}"
(Might be a runaway multi-line << string starting on line 25)
Execution of x.pl aborted due to compilation errors.
I am not sure as to why, as the syntax seems fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜想这些
=20
实例位于消息正文中。 阅读一点 Mail::Message 文档< /a> 将显示这个有用的注释:因此,无需在循环中调用
$msg->body
,只需调用$msg->decoded->string
即可。使用
Mail::Message::timestamp
时,完成排序应该是最简单的:I guess that those instances of
=20
are in the body of the message. Reading just a bit of the documentation for Mail::Message will reveal this helpful note:Thus instead of calling
$msg->body
in your loop, simply call$msg->decoded->string
.Accomplishing sorting should be easiest when you use
Mail::Message::timestamp
:我建议查看 MIME::Base64模块,其中包括用于解码 QP 主体的 MIME::QuotedPrint::Perl 模块。
I'd suggest to look at the MIME::Base64 module which includes MIME::QuotedPrint::Perl module to decode QP bodies.