2010-01-12 75 views

回答

13

簡單,$樹沒有堅持過去的eval {}。通常,perl中的括號總是提供一個新的範圍。並警告要求您提供參數$ @。

my $tree; 
eval { 
    # parses the file contents into the new libXML object. 
    $tree = $parser->parse_file($file) 
}; 
warn [email protected] if [email protected]; 
+4

有沒有必要單獨申報$樹。 eval的結果是最後評估的結果:my $ tree = eval {...}。 – 2010-01-12 11:26:48

5

你正在大括號內聲明一個$樹,這意味着它不會超過右大括號。試試這個:

use XML::LibXML; 
my $parser = XML::LibXML->new(); 

my $tree; 
eval { 
    $tree = $parser->parse_file($file) # parses the file contents into the new libXML object. 
}; 
warn("Error encountered: [email protected]") if [email protected];