2013-09-30 72 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