Perl 在解析包含正则表达式的 json 时内存不足



我正在解析一个非常简单的json文件,perl内存不足。我正在使用来自cpan的JSON库。我正在从名为"data.json"的文件中读取 json。我也在使用File::Slurper模块。

use JSON;
use File::Slurper;
my $file = read_text("data.json");
my @data = decode_json($file);
print @data;

在我的data.json文件中,我有一个对象数组,每个 obj 中都有正则表达式。

[
{
"val": "test",
"reg": "m/^(match)/"
}
...3 more entries
]

我运行这个它说

内存不足!

我怎样才能绕过这个?

目前尚不清楚是什么导致了代码中的错误。请参阅以下示例代码,该代码以略有不同的方式执行相同的工作。

use strict;
use warnings;
use feature 'say';
use JSON;
use Data::Dumper;
my $file = do { local $/; <DATA> };
my @data = decode_json($file);
say Dumper(@data);
__DATA__
[
{
"val": "test1",
"reg": "m/^(match)/"
},
{
"val": "test2",
"reg": "m/^(match)/"
},
{
"val": "test3",
"reg": "m/^(match)/"
},
{
"val": "test4",
"reg": "m/^(match)/"
}
]

输出

$VAR1 = [
[
{
'val' => 'test1',
'reg' => 'm/^(match)/'
},
{
'val' => 'test2',
'reg' => 'm/^(match)/'
},
{
'val' => 'test3',
'reg' => 'm/^(match)/'
},
{
'reg' => 'm/^(match)/',
'val' => 'test4'
}
]
];

最新更新