如何按日期对 mbox 文件进行排序?

发布于 2024-07-09 23:01:34 字数 1045 浏览 18 评论 0原文

我想知道如何修改下面的代码以在多行末尾去除 =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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我一直都在从未离去 2024-07-16 23:01:34

我猜想这些 =20 实例位于消息正文中。 阅读一点 Mail::Message 文档< /a> 将显示这个有用的注释:

请注意,这会返回一个可能被编码的对象:使用decoded() 获取包含可用数据的主体。

因此,无需在循环中调用 $msg->body,只需调用 $msg->decoded->string 即可。

使用 Mail::Message::timestamp 时,完成排序应该是最简单的:

...
for my $msg ( sort { $a->timestamp <=> $b->timestamp } $folder->messages) )
...

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:

BE WARNED that this returns you an object which may be encoded: use decoded() to get a body with usable data.

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:

...
for my $msg ( sort { $a->timestamp <=> $b->timestamp } $folder->messages) )
...
一曲琵琶半遮面シ 2024-07-16 23:01:34

我建议查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文