2

我正在使用 XML::DOM 编写 Perl 代码。

我在 Perl help(XML::DOM::Node) 中找到了如下说明。

@list = $node->getChildNodes;        # returns a perl list
$nodelist = $node->getChildNodes;    # returns a NodeList (object reference)
for my $kid ($node->getChildNodes)   # iterate over the children of $node

如果我将它传递给我的函数,如下所示。

&myfunction($node->getChildNodes)

我没有得到对列表的引用。

有什么方法可以在不声明临时标量变量的情况下获取引用,如下所示。

my $tmp=$node->getChildNodes;
&myfunction($tmp)

谢谢你。

4

1 回答 1

1
myfunction([ $node->getChildNodes ])

myfunction(scalar $node->getChildNodes)

第二个避免创建新数组和新引用,但第一个可能更清晰。


PS——你为什么告诉 Perl 忽略原型myfunction?不要使用&.

于 2012-11-27T15:46:46.787 回答