2

我正在尝试使用以下代码,但我还无法测试,因为我收到以下错误:

#!/usr/bin/perl
use warnings;
use strict;
use Text::Wrap;
use Mail::Box::Manager;
use HTML::Obliterate qw(extirpate_html);


open (MYFILE, '>>data.txt');
binmode(MYFILE, ':encoding(UTF-8)');


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 ( sort { $a->timestamp <=> $b->timestamp } $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->decoded->string;


if ( $msg->isMultipart ) {
    foreach my $part ( $msg->parts ) {
        if ( $part->contentType eq 'text/html' ) {
          my $nohtml = extirpate_html( $msg );
$body =~ s/^>.*$//msg;
$Text::Wrap::columns=80;
print MYFILE wrap("", "", <<"");
\n
From: $from
To: $to
Date: $date
Subject: $subject
\n
$body
        }

else {


$body =~ s/^>.*$//msg;
$Text::Wrap::columns=80;
print MYFILE wrap("", "", <<"");
\n
From: $from
To: $to
Date: $date
Subject: $subject
\n
$body

}

}}

所有的大括号似乎都匹配,所以我不确定问题出在哪里

syntax error at x.pl line 46, near "else"
  (Might be a runaway multi-line << string starting on line 36)
Missing right curly or square bracket at x.pl line 63, at end of line
syntax error at x.pl line 63, at EOF
Execution of x.pl aborted due to compilation errors.

编辑:

它现在可以工作了,但是 html 没有条带化:取而代之的是几封电子邮件,其中包含
>
> 之类的东西交错贯穿,导致它的页面比应有的多得多。有一个更好的方法吗

4

2 回答 2

4

所以第36行似乎是

print MYFILE wrap("", "", <<"");

这意味着 perl 将包装以下文本,直到出现终止符""(我从不使用像这样令人困惑的项目,为了简单起见,我总是使用 END 或 UNTIL_END。)

然后在第 45 行(空行)找到该终止符,这意味着它处理的下一件事是第 46 行:

else {

这是没有意义的,因为前一个if还没有关闭(有 } 的第 44 行在终止符之前"",因此它被视为用于换行的文本。Perl 注意到了这一点,并建议你这可能是罪魁祸首:

(Might be a runaway multi-line << string starting on line 36)

您需要交换第 44 和 45 行以首先具有终止符""(空行),然后关闭 if 与}. 您示例中的第二个包装正确地做到了这一点。

于 2008-12-16T15:39:10.243 回答
1

回答您修改后的问题:

而不是消除消息正文,而是消除整个消息。然后你不会在任何地方使用它。

my $nohtml = extirpate_html( $msg );
$body =~ s/^>.*$//msg;
$Text::Wrap::columns=80;
print MYFILE wrap("", "", <<"");
\n
From: $from
To: $to
Date: $date
Subject: $subject
\n
$body

也许您需要将其更改为:

my $nohtml = extirpate_html( $body );
$nohtml =~ s/^>.*$//msg;

然后将其应用$nohtml为消息正文进行换行。

于 2008-12-16T16:10:22.397 回答