Perl项目维护日志

There's more than one way to do it!
https://metacpan.org http://perlmonks.org
回复
头像
523066680
Administrator
Administrator
帖子: 573
注册时间: 2016年07月19日 12:14
联系:

Perl项目维护日志

帖子 523066680 »

记录一些容易犯的错误,主要目的是创建一些规则,以避免重蹈覆辙

头像
523066680
Administrator
Administrator
帖子: 573
注册时间: 2016年07月19日 12:14
联系:

Re: Perl项目维护日志

帖子 523066680 »

代码: 全选

                # 向上寻找对应 $tag 元素的节点,$tag = "Group Mask" 的情况
                my $tag1 = split(/ |,/, $tag);
                $sect = $insert->parent->previous;
                while ( defined $sect and ($sect->tag ne $tag1) )
                {
                    $sect = $sect->previous;
                }

            if (not defined $sect)
            {
                printf "Insert_Attr 未找到对应 tag %s\n%s\n", $tag, $insert->parent->previous;
                $insert->remove();
                next;
            }

在一次 XML DOM处理中,始终没有得到正确结果,while 语句块被反复运行。
原因是 my $tag1 = split(/ |,/, $tag); 始终返回结果为1
split 函数,如果是标量环境,返回数组的大小数值;要获取split返回的第一个元素,正确的方式是创建数组环境:
my ($tag1) = split(/ |,/, $tag);
事实上我在编写时考虑过这个问题,但是竟然没把括号加上。
查BUG的过程绕了很大一圈。

为了避免再次出现类似的情况(忘了加括号),我反思一下,下次或许应该这么写:

代码: 全选

my $tag1 = (split(/ |,/, $tag))[0];
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 3 访客