简体   繁体   English

将Perl哈希加载到Java中

[英]Load a Perl Hash into Java

I have a big .pm File, which only consist of a very big Perl hash with lots of subhashes. 我有一个很大的.pm文件,其中仅包含一个非常大的Perl哈希,其中包含很多子哈希。 I have to load this hash into a Java program, do some work and changes on the data lying below and save it back into a .pm File, which should look similar to the one i started with. 我必须将此哈希值加载到Java程序中,对下面的数据进行一些工作和更改,然后将其保存回.pm文件中,该文件看起来应该与我最初使用的文件类似。

By now, i tried to convert it linewise by regex and string matching, converting it into a XML Document and later Elementwise parse it back into a perl hash. 到目前为止,我尝试通过正则表达式和字符串匹配将其逐行转换,将其转换为XML文档,然后通过Elementwise将其解析回perl哈希。

This somehow works, but seems quite dodgy. 这在某种程度上是可行的,但是似乎很狡猾。 Is there any more reliable way to parse the perl hash without having a perl runtime installed? 在没有安装Perl运行时的情况下,还有什么更可靠的方法来解析Perl哈希?

You're quite right, it's utterly filthy. 您说得对,完全是肮脏的。 Regex and string for XML in the first place is a horrible idea, and honestly XML is probably not a good fit for this anyway. 首先,用于XML的正则表达式和字符串是一个可怕的想法,老实说XML可能无论如何都不适合。

I would suggest that you consider JSON . 我建议您考虑使用JSON I would be stunned to find java can't handle JSON and it's inherently a hash-and-array oriented data structure. 我会惊讶地发现java无法处理JSON ,它本质上是面向哈希和数组的数据结构。

So you can quite literally: 因此,您可以从字面上看:

use JSON;
print to_json ( $data_structure, { pretty => 1 } );

Note - it won't work for serialising objects, but for perl hash/array/scalar type structures it'll work just fine. 注意-它不适用于序列化对象,但对于perl哈希/数组/标量类型结构,它将很好用。

You can then import it back into perl using: 然后可以使用以下命令将其导入回perl:

my $new_data = from_json $string;
print Dumper $new_data;

Either Dumper it to a file, but given you requirement is multi-language going forward, just using native JSON as your 'at rest' data is probably a more sensible choice. 可以将其Dumper到文件中,但是鉴于您的要求是多语言的发展,仅使用本地JSON作为“静态”数据可能是更明智的选择。

But if you're looking at parsing perl code within java, without a perl interpreter? 但是,如果您要在没有Perl解释器的情况下在Java中解析Perl代码 No, that's just insanity. 不,那只是精神错乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM