我有一条 HTML 和 TEXT Mime 消息。我想删除 HTML 部分。看起来这可以解决问题:
### Delete some parts of a multipart message:
my @keep = grep { keep_part($_) } $msg->parts;
$msg->parts(\@keep);
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何阅读这个,或者真的不知道该怎么称呼它(回调?)..我可以按如下方式找到该部分:
for my $part ($msg->parts()) {
if ($part->mime_type eq 'text/html') {
Run Code Online (Sandbox Code Playgroud)
当您向函数提供ARRAYREFof时,它会将对象设置为恰好包含这些s。所有先前的实体都被删除。MIME::EntitypartsMIME::Entity
@keep示例中的数组包含返回 true 的那些s ,并且MIME::Entity在中设置新的s 。keep_part$msg->parts(\@keep);MIME::Entity$msg
因此,的实现keep_part可能是这样的:
sub keep_part {
shift->mime_type ne 'text/html';
}
Run Code Online (Sandbox Code Playgroud)
也就是说,如果提供的参数的 mime 类型不是 ,它将返回true text/html。
如果条件并不比这更复杂,您可能只想内联过滤它:
# create an ARRAYREF to the parts to keep and set the parts in $msg:
$msg->parts([ grep { $_->mime_type ne 'text/html' } $msg->parts]);
Run Code Online (Sandbox Code Playgroud)
过滤完成后,您无需if ($part->mime_type eq 'text/html')再检查循环。所有text/htmlMIME 实体都将被删除:
for my $part ($msg->parts) {
print $part->mime_type . "\n"; # no text/html
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |