2013-09-30 41 views
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}; 

然而,当我这样做,我得到和错误值未初始化。但是当我做dump的只是$ent->parts->($i)我得到了上面的代码。

我只是停留在这一个。 谢谢, 利奥Ç

+5

在另一方面,它可能会使用访问器方法访问对象的属性更清洁,而不是直接获取值通过散列。 [封装(http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29)。 – Nikhil

回答

5

你没有一个哈希,你有一个对象(这恰好是实现为散列)。这就是为什么Data :: Dumper输出持续说bless(...)。你不应该去探索它的内部。

我认为你正在寻找

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

此:

print $ent->parts->($i)->{ME_Parts}->[$i]->{ME_Bodyhandle}->{MB_Path}; 
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