简体   繁体   中英

Sharing data structures between perl and cpp

I have a perl script which generates a very large data structure (which starts life as an array of array references). This is then written to a text file using some weird home-brew serialisation scheme.

The data from the text file is stored as the value in a key-value store db.

A c++ file then retrieves the data, and deserializes it (into a hashmap, although can potentially be flexible on how this data is structured).

What I'm interested in is finding if there are any good ways of sharing a data structure between perl and c++ (something like Storable , but that is meant for perl->perl not perl->c++). The current method is a headache to maintain, and may not have the best performance.

The most important factors are speed of deserialisation, and the size of the serialized structure in that order. Anyone know of something that might do the trick?

Storable is one way to dump and load perl data structures. I wouldn't actually recommend it for general usage though - it's handy in that it's part of core and easy to use.

But for multi-platform (and language) portability, it's far better to use a standard data representation. Which you choose is probably a matter of what sort of data you're holding in your structure, but core contenders are:

  • JSON - good for arrays and hashes (key-value).
  • YAML - Excellent for 'config file' style data (but extends in ways similar to JSON)
  • And if you must, XML - but bear in mind that XML is designed for documents-with-metadata, and so IMO isn't suitable for most of the applications it's used for.

As standards, they've got documented formatting and parsers are widely available. And implementing your own isn't too hard, if that's the route you want to go. Just make sure you follow the spec and you're good.

Note - that because XML and JSON (and I think YAML?) are recursive, you can parse as a stream, rather than a standalone object. (Trap, process and discard as you hit 'close brackets' in JSON, or 'close tags' in XML).

easy job.

I like perl , and I also like C/C++. To make the best of both, I wrote a github project to solve this issue.

please see: https://github.com/tlqtangok/perlcpp

a short example is here :

P_eval("$a=2;$a=$a**10;"); 
Int("a") ;   // a= 1024

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