bansalam has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have a perl module calling a java API which returns a hashmap. Now i want to convert this to perl hashes. What is the best way to do it? Also, for faster processing, i want to convert the java side hashmap into string form and return the same. I was thinking if eval can be used to generate hashes from this string? What can be the string format formed at java end using the hashamp such that eval can directly convert it to perl hashes?

Replies are listed 'Best First'.
Re: COnvert java hashmap to perl hashes
by Corion (Patriarch) on Jun 16, 2015 at 13:59 UTC

    The approach best supported by libraries and tooling currently is JSON. For Java there are likely many good libraries to convert a hashmap to a valid JSON document.

Re: COnvert java hashmap to perl hashes
by robby_dobby (Hermit) on Jun 17, 2015 at 05:15 UTC
    Hello bansalam,

    Expanding on what Corion said, here's the part where you need some form of serialization since this is boils down to communication between two different (or nearly same - how you look at it) cultures.

    JSON is certainly one way to serialize data between two different programs, there are others too. For example, XML, YAML and Google's protocol buffers are all valid solutions. You can even write out the Java HashMap to a text file(HashMap's toString() should help) and read from that file using perl. Of course, there are modules available on CPAN for every one of these formats. JSON, YAML and Google::ProtocolBuffers come to mind.

    Pick the simplest and get to work. Have fun! :-)