【发布时间】:2016-08-15 18:42:59
【问题描述】:
我正在尝试在 Perl 中打印一些基本日志,但我遇到了一个非常简单的问题:我无法打印 XML 标记的内容。
my $twig=XML::Twig->new(pretty_print => "nice");
$twig->parse($xml);
my $root = $twig->root;
my @desc=$root->descendants_or_self('node');
my $nrofdesc=@desc;
my $sentence = $root->descendants('sentence')->print;
my $sentenceid = $root->{att}->{id};
if ($nrofdesc > $maxdescendants) {
print "$sentence\t$nrofdesc\t$sentenceid\n";
}
我尝试了上面的代码,但我收到了错误
不能在没有包或对象引用的情况下调用方法“print” file.pl 第 35 行,第 15 行。
这是哪一行:
my $sentence = $root->descendants('sentence')->print;
我也尝试了text,正如经常提出的那样,但我得到了同样的错误。我在这里错过了什么?
【问题讨论】:
-
根据 perldoc
descendants会给你一个列表,而不是一个对象。可能您需要查看打印单个“树枝”的列表。