1

我继承了一个脚本,我需要能够从哈希中访问一些数据。我希望能够从以下访问 MB_Path 值。

$VAR1 = bless(
    {  
        'ME_Parts' => [
            bless(
                {  
                    'ME_Bodyhandle' => bless(
                        {  
                            'MB_Path' => '/tmp/msg-15072-1.txt'
                        },
                        'MIME::Body::File'
                    ),
                    'ME_Parts'       => [],
                    'mail_inet_head' => bless(
                        {  
                            'mail_hdr_foldlen' => 79,
                            'mail_hdr_modify'  => 0,
                            'mail_hdr_list'    => [
                                'Content-Type: text/plain; charset="us-ascii"',
                                'Content-Transfer-Encoding: quoted-printable'
                            ],
                            'mail_hdr_hash' => {
                                'Content-Type' => [
                                    \$VAR1->{'ME_Parts'}[0]{'mail_inet_head'}
                                      {'mail_hdr_list'}[0]
                                ],
                                'Content-Transfer-Encoding' => [
                                    \$VAR1->{'ME_Parts'}[0]{'mail_inet_head'}
                                      {'mail_hdr_list'}[1]
                                ]
                            },
                            'mail_hdr_mail_from' => 'KEEP',
                            'mail_hdr_lengths'   => {}
                        },
                        'MIME::Head'
                    )
                },
                'MIME::Entity'
            ),
            bless(
                {  
                    'ME_Bodyhandle' => bless(
                        {   
                            'MB_Path' => '/tmp/msg-15072-2.html'
                        },  
                        'MIME::Body::File'
                    ),  
                    'ME_Parts'       => [], 
                    'mail_inet_head' => bless(
                        {   
                            'mail_hdr_foldlen' => 79, 
                            'mail_hdr_modify'  => 0,
                            'mail_hdr_list'    => [
                                'Content-Type: text/html;charset="us-ascii"',
                                'Content-Transfer-Encoding: quoted-printable'
                            ],
                            'mail_hdr_hash' => {
                                'Content-Type' => [
                                    \$VAR1->{'ME_Parts'}[1]{'mail_inet_head'}
                                      {'mail_hdr_list'}[0]
                                ],
                                'Content-Transfer-Encoding' => [
                                    \$VAR1->{'ME_Parts'}[1]{'mail_inet_head'}
                                      {'mail_hdr_list'}[1]
                                ]
                            },
                            'mail_hdr_mail_from' => 'KEEP',
                            'mail_hdr_lengths'   => {}
                        },
                        'MIME::Head'
                    )
                },
                'MIME::Entity'
            )
        ],
        'ME_Epilogue'    => [],
        'ME_Preamble'    => [],
        'mail_inet_head' => bless(
            {
                'mail_hdr_foldlen' => 79,
                'mail_hdr_modify'  => 0,
                'mail_hdr_list'    => [
'Content-Type: multipart/alternative;boundary="----_=_NextPart_002_01CEB949.DC6B0180"'
                ],
                'mail_hdr_hash' => {
                    'Content-Type' =>
                      [ \$VAR1->{'mail_inet_head'}{'mail_hdr_list'}[0] ]
                },
                'mail_hdr_mail_from' => 'KEEP',
                'mail_hdr_lengths'   => {}
            },
            'MIME::Head'
        )
    'MIME::Entity'
);

我以为我可以简单地执行以下操作

print $ent->parts->($i)->{ME_Bodyhandle}->{MB_Path};

但是,当我这样做时,我得到并错误地指出该值未初始化。但是当我转储时,$ent->parts->($i)我得到了上面的代码。

我只是坚持这个。谢谢,狮子座C

4

3 回答 3

5

你没有散列,你有一个对象(它恰好被实现为散列)。这就是为什么 Data::Dumper 输出一直说bless(...). 你不应该戳到它的内部。

我想你正在寻找

$ent->parts($i)->bodyhandle->path;
于 2013-09-30T18:36:17.940 回答
4

在您用尽文档的所有可能性之前,没有任何借口可以转储表示 Perl 对象的底层数据结构和对其组件的硬编码访问。封装规则适用于 Perl 面向对象编程与任何其他语言一样多。

MIME::Entity 和 的文档 MIME::Body 很清楚,你需要的代码是这样的

for my $part ($ent->parts) {
  my $path = $part->bodyhandle->path;
  print $path, "\n";
}

输出

/tmp/msg-15072-1.txt
/tmp/msg-15072-2.html
于 2013-09-30T19:04:44.227 回答
0

这个:

print $ent->parts->($i)->{ME_Parts}->[$i]->{ME_Bodyhandle}->{MB_Path};
于 2013-09-30T18:13:42.323 回答