简体   繁体   中英

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. 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.

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.

This somehow works, but seems quite dodgy. Is there any more reliable way to parse the perl hash without having a perl runtime installed?

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.

I would suggest that you consider JSON . I would be stunned to find java can't handle JSON and it's inherently a hash-and-array oriented data structure.

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.

You can then import it back into perl using:

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.

But if you're looking at parsing perl code within java, without a perl interpreter? No, that's just insanity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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