parse-6ren"> parse-我有 Javascript 代码,我需要将 javascript 变量解析为 Perl 哈希。有现成的模块吗?我试过JE::parse()和 JavaScript::HashRef::Decode但都-6ren">
gpt4 book ai didi

javascript - 使用 Perl 解析 JavaScript : getting "undef" from JE->parse

转载 作者:行者123 更新时间:2023-11-28 14:18:16 26 4
gpt4 key购买 nike

我有 Javascript 代码,我需要将 javascript 变量解析为 Perl 哈希。有现成的模块吗?我试过JE::parse()JavaScript::HashRef::Decode但都不起作用。

预期行为:

use Data::Dumper;
use SomeModule::ParseJSVariables qw/decode_js/;

my $str = qq/
var data = {
'abc': 1,
'def' : 2
'xyz' : { 'foo' : 'bar' }
}
/;

my $res = decode_js($str);
warn Dumper $res; #

# expected result:
# {
# name => 'data',
# value => {
# 'abc' => 1,
# 'def' => 2
# 'xyz' => { 'foo' => 'bar' }
# }
# }


use JE;
my $j = new JE;
my $parsed = $j->parse($str);
warn Dumper $parsed; # undef :(

如果没有现成的模块,如果有人建议正确的正则表达式或解析方法,我将很高兴。

UPD,澄清。我有大约一千行 JavaScript 代码,我只需要获取在全局范围内明确给出的变量的内容,例如 var x = { 'foo' : 'bar' } 。其他代码可以跳过解析。

我的环境:

$ perl --version

This is perl 5, version 22, subversion 1 (v5.22.1) built for x86_64-linux-gnu-thread-multi

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.5 LTS
Release: 16.04
Codename: xenial

$ uname -r
4.19.24-041924-generic

最佳答案

好吧,我有好消息和坏消息,而且它们是同一个消息:)。您的 JS 有语法错误,因此 JE 根据 the JE docs 返回 undef 。具体来说,def 行缺少 ,。以下测试对我有用:

代码:

use Data::Dumper;

# Note: use q[ ] instead of qq/ /. q instead of qq so Perl doesn't interpolate
# into the contents, and [ ] instead of / / so that JS comments can appear
# in the block.
my $str = q[
var data = {
'abc': 1,
'def' : 2, // <==== There was a comma missing here!
'xyz' : { 'foo' : 'bar' }
}
];

use JE;
my $j = new JE;
my $parsed = $j->parse($str);
warn Dumper $parsed;

输出:太大,无法在此处显示:)。但它确实包含了你想要的!

提取输出

这将是一个挑战。希望以下内容能够帮助您入门。

代码:

use Data::Dumper::Compact 'ddc';   # <== for briefer output
use JE;

# Note: use q[ ] instead of qq/ /. q instead of qq so Perl doesn't interpolate
# into the contents, and [ ] instead of / / so that JS comments can appear
# in the block.
my $str = q[
var data = {
'abc': 1,
'def' : 2, // <==== There was a comma missing here!
'xyz' : { 'foo' : 'bar' }
}
];

my $j = new JE;
my $parsed = $j->parse($str);
print ddc $parsed->{tree}; # <== {tree} holds the parsed source

输出(带注释):

bless( [
[
0,
118,
],
"statements",
bless( [
[
1,
118,
],
"var",
[
"data",
bless( [
[
12,
117,
],
"hash", <== here's where your hash starts
"abc", <== 'abc': 1
1,
"def", <== 'def': 2
2,
"xyz", <== 'xyz': nested hash
bless( [
[
98,
115,
],
"hash",
"foo",
"sbar",
], 'JE::Code::Expression' ),
], 'JE::Code::Expression' ),
],
], 'JE::Code::Statement' ),
], 'JE::Code::Statement' )

关于javascript - 使用 Perl 解析 JavaScript : getting "undef" from JE->parse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56440992/

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