gpt4 book ai didi

xml - 带有 XML Twig 段错误的 Perl 脚本,子进程以信号 11 终止

转载 作者:数据小太阳 更新时间:2023-10-29 02:40:53 25 4
gpt4 key购买 nike

我正在尝试运行一个 perl 脚本,其中包含一些正在构建的 Twig 。此脚本应获取 xml 文件并返回文件中作为属性存在的版本号。每次我尝试解析一个大文件 (23 MB) 时,脚本都会崩溃并显示以下内容 -

"Child 341 terminated with signal 11".

调用将获得所需属性的子例程的代码-

my $version = $strm_obj->get_attr(file=>$file1,tag=>"config",attr=>"contentversion");
print "Version of $file1 is $version \n";
my $globalversion = $strm_obj->get_attr(file=>$file2,tag=>"config",attr=>"globalcontentversion");
print "Version of $file2 is $globalversion \n";

获取所需属性的子程序 -

sub get_attr{
my ($self,%args) = @_;
my $file = $args{file};
my $tag = $args{tag};
my $attr = $args{attr};
my $val;
$self->{_ATTR} = $attr;
$self->{_TAG} = $tag;
test_log(DEBUG,"Value of tag is $tag, attribute is $attr");
my $twig= XML::Twig->new(
twig_roots => { $tag
=> sub {$self->get_attr_helper(@_,$tag,\$val); } } )
->parsefile($file);
if ($val){
test_log(INFO,"value of attribute $attr is $val");
}
if (!$val){
test_log(INFO,"The attribute $attr that you are looking for, is not present in $file");
return -1;
}
$twig->purge;
$twig->dispose;
return $val;
}

sub get_attr_helper{
my($self,$obj,$tag,$act_tag,$val) = @_;
my $attr = $self->{_ATTR};
#print "my attr is $attr\n";
for my $node ($tag->findnodes("//$self->{_TAG}")){
if ($node->att("$attr")){
$$val = $node->att("$attr");
}
}
$obj->purge;
}

xml文件格式如下:

$文件1 -

<config contentversion="378">
<tag1>
.
.
.
<tag n>
</config>

$文件2-

<config globalcontentversion="378">
<tag1>
.
.
.
<tag n>
</config>

我真的不能在这里提供实际的 xml 文件。

我知道这个脚本最多占用我机器大约 20% 的内存(2GB RAM)。

我环顾四周,一直找不到解决办法。

如何消除段错误?

最佳答案

很难给出具体的答案,因为段错误意味着某些东西乱七八糟地坏了(这是一个基于内存的问题)。

XML 很容易占用大量内存,在很大程度上,XML::Twig 的最大优势之一是它能够使用 twig_handlers 进行解析和丢弃清除

这使得它非常适合从 XML 中部分提取内容。

我看不出具体是什么导致了段错误,但是 - 在 perl 中,您不会经常遇到段错误,这很可能是外部原因。

除此之外 - 您似乎正在做一些非常复杂的事情来从您的文件中提取版本号。 (这是假设我没有误读您要提取的内容)。

这样的东西不符合您的需求吗?:

use strict;
use warnings;
use XML::Twig;

sub get_attr {
my ( $self, %args ) = @_;
my $file = $args{file};
my $tag = $args{tag};
my $attr = $args{attr};

my $twig = XML::Twig->new()->parsefile($file);

my $val = $twig->root->first_child($tag)->att($attr);
#maybe error check to see if 'first_child($tag)' is defined first?

return $val;
}

尽管如果您的“doc root”始终是您要提取的“config”分支,您可以进一步简化:

my $val  = $twig->root->att($attr);

我试过了 - 它适用于您目前提供的两个样本。如果您仍然出现段错误,我会考虑检查您安装的内容。

(可能值得采用“ Twig 处理程序”方法来捕获标签,但我认为这不是特别必要,因为最大的优势是可以边走边清除,这似乎没有必要,因为问题的大小)。

XML::Twig 中列出了一个错误:

http://search.cpan.org/~mirod/XML-Twig-3.48/Twig.pm#BUGS

segfault during parsing This happens when parsing huge documents, or lots of small ones, with a version of Perl before 5.16.

This is due to a bug in the way weak references are handled in Perl itself.

The fix is either to upgrade to Perl 5.16 or later (perlbrew is a great tool to manage several installations of perl on the same machine).

An other, NOT RECOMMENDED, way of fixing the problem, is to switch off weak references by writing XML::Twig::_set_weakrefs( 0); at the top of the code. This is totally unsupported, and may lead to other problems though,

虽然我不太确定这是否适用于您,因为我真的不会将“23MB”称为巨大的 XML。 (请记住,XML 的内存占用量约为 10 倍)。

关于xml - 带有 XML Twig 段错误的 Perl 脚本,子进程以信号 11 终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20894222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com