【发布时间】:2014-12-31 09:33:15
【问题描述】:
在我的一小段代码中,我解析了一些 XML 数据,但是由于我需要在 3 个地方做同样的事情,所以我想为每次做一个子例程。但是我需要将我正在处理的当前节点作为参数传递,超出了我目前的技能来访问当前节点的子节点。
这是我的代码示例:
foreach $day ($doc->findnodes('/my/current/path')) {
@atts = $day->getAttributes();
foreach $at (@atts) {
$na = $at->getName();
$va = $at->getValue();
if ($va eq "today") {
#------ my repeated code begins here -----
foreach $thing ($day->findnodes('child_nodes_im_looking_for')) {
#----- do a lot of stuff
}
#------ my repeated code ends here -----
}
if ($va eq "tomorrow") {
#same repeated code
}
if ($va eq "some_other_day") {
#same repeated code.... again
}
#for other days... do nothing
}
我应该如何将当前节点传递给子例程,以便我可以直接从例程访问其子节点?
【问题讨论】: