2010-01-12 26 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];