简体   繁体   English

在Perl和CPP之间共享数据结构

[英]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). 我有一个perl脚本,它会生成一个非常大的数据结构(它开始时是数组引用的数组)。 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. 来自文本文件的数据作为值存储在键值存储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). 然后,一个c ++文件检索该数据,并将其反序列化(生成一个哈希表,尽管可以灵活地构造该数据)。

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++). 我感兴趣的是,找到是否有在perl和c ++之间共享数据结构的好方法(类似于Storable ,但这是为了perl-> perl而不是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. Storable是转储和加载perl数据结构的一种方法。 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). JSON-适用于数组和哈希(键值)。
  • YAML - Excellent for 'config file' style data (but extends in ways similar to JSON) YAML-非常适合“配置文件”样式数据(但以类似于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. 而且,如果需要的话,可以使用XML-但请记住,XML是为带有元数据的文档设计的,因此IMO不适合用于它的大多数应用程序。

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. 注意-因为XML和JSON(我认为是YAML?)是递归的,所以可以将其解析为流,而不是独立的对象。 (Trap, process and discard as you hit 'close brackets' in JSON, or 'close tags' in XML). (当您点击JSON中的“右括号”或XML中的“关闭标签”时,请进行捕获,处理和丢弃)。

easy job. 简单的工作。

I like perl , and I also like C/C++. 我喜欢perl,我也喜欢C / C ++。 To make the best of both, I wrote a github project to solve this issue. 为了充分利用两者,我编写了一个github项目来解决此问题。

please see: https://github.com/tlqtangok/perlcpp 请参阅: https : //github.com/tlqtangok/perlcpp

a short example is here : 一个简短的例子在这里:

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

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

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