如何编译加载到字符串中的erlang代码?
我有一个生成的字符串,其中包含 erlang 模块的代码。
有没有办法直接从字符串编译生成的模块?
或者有没有办法将字符串转换为 compile:forms/1
所需的格式?
或者我是否必须先将其保存到临时文件,然后使用 compile:file/1
进行编译?
或者,我可以添加对编译模块的支持,但是编写 erlang 的好人没有添加它肯定是有原因的。
I have a generated string that contains the code for an erlang module.
Is there a way to compile the generated module straight from the string?
Or is there a way to convert the string into the format needed for compile:forms/1
?
Or will I have to save it to a temp file first and then compile it with compile:file/1
?
Alternatively, I can add support to the compile module, but there must be a reason why the good people that write erlang haven't added it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将字符串扫描为标记,然后将标记解析为表单。
不幸的是,一次只能解析一种表单,因此您需要在表单边界处剪切字符串或标记。这是一个简短的示例:
或者,您可以扫描字符串,然后将生成的标记列表以
{dot, _}
标记分开。You need to scan your string into tokens, and then parse the tokens into forms.
Unfortunately it is only possible to parse one form at a time, so you will need to either cut your string, or your tokens at form boundaries. Here is a short example:
Alternatively you can scan your string, and then cut the resulting token list at
{dot, _}
tokens apart.将字符串的内容存储在具有模块名称的文件中(如果代码中没有)并使用 编译模块。
代码在虚拟机中变得可用。我正在使用这种技术将配置文件转换为模块。这样,在执行过程中就不会进行太多的二进制复制。
Store the content of the string in a file with a module name (if not there in the code) and compile it using compile module.
Code becomes available in the VM. I am using this technique to convert a configuration file into a module. This way there not much binary copy going on during the execution.