0

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?

chenino
  • 454
  • 2
  • 7
  • 19

1 Answers1

1

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.

Sobrique
  • 52,974
  • 7
  • 60
  • 101
  • 1
    I was hoping maybe there is a perl Interpreter library for Java, as i have found None. But your answer makes me more doubting, that there is any – chenino Dec 02 '16 at 14:48
  • I think it phenomenally unlikely, because `perl` is the "perl interpreter" that you're looking for. I can't imagine anyone rewriting perl as a java library just because they don't want to install `perl` _and_ `java`. – Sobrique Dec 02 '16 at 14:49
  • Yeah, if it wouldn't had to be Plattform Independent, i would include a local perl Parser, as reading a hash does not need many packages. Well at least the hash has a consisten structure, so parsing it and reading key & value from it is somehow possible, but i am afraid of users entering bad written hashes – chenino Dec 02 '16 at 14:51
  • How are the hashes 'written'? An export-via-JSON really shouldn't be hard. Or for the really filthy option - POST it via a CGI webserver, to jsonify the hash code :) – Sobrique Dec 02 '16 at 14:54
  • Those hashes are written on a different machine and contain configuration Settings, so they might also be manipulated by users manually. Exporting them to JSON while creation is impossible like this as i can not integrate into this flow – chenino Dec 02 '16 at 14:59