坚持尝试访问哈希值

时间:2013-09-30 18:10:12

标签: perl

我继承了一个脚本,我需要能够从哈希中访问一些数据。我希望能够从以下内容访问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)时,我得到了上面的代码。

我只是坚持这个。 谢谢, Leo C

3 个答案:

答案 0 :(得分:5)

你没有哈希,你有一个对象(碰巧实现为哈希)。这就是Data :: Dumper输出继续说bless(...)的原因。你不应该盯着它的内部。

我认为你正在寻找

$ent->parts($i)->bodyhandle->path;

答案 1 :(得分:4)

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

的文档 MIME::EntityMIME::Body 非常清楚,你需要的代码是这样的

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

<强>输出

/tmp/msg-15072-1.txt
/tmp/msg-15072-2.html

答案 2 :(得分:0)

此:

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