寻找 eval 的替代方案

发布于 2024-09-10 17:58:31 字数 464 浏览 2 评论 0原文

我是 ruby​​ 新手,但是 perl 的变化并不是很大,无论如何,我已经编写了一个简单的脚本来将我的 perl Data::Dumper 输出转换为 yaml 配置,我的问题是我正在使用eval 来完成这一任务,并且看到我可能希望其他人有一天使用这个脚本,我想消除 eval 以获得更理智的东西。

示例:

输入文件包含

$VAR1 = { 'object' => { 'some_key' => 'some_value' } }

中读取它的方法

# read in file here ...
eval( stringified_file )
print $VAR1.to_yaml

在输出

object:
  some_key: some_value

谢谢:)

I'm new to ruby however it isn't really that drastic of a change coming from perl, anyways 've written a simple script to convert my gobs of perl Data::Dumper output into yaml configs, my problem is I'm using eval to accomplish this and seeing as I may like others to use this script one day I would like to eliminate eval for something more sane.

example:

input file contains

$VAR1 = { 'object' => { 'some_key' => 'some_value' } }

method to read it in

# read in file here ...
eval( stringified_file )
print $VAR1.to_yaml

output

object:
  some_key: some_value

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2024-09-17 17:58:31

在 Perl 端,您可以将数据结构输出到 YAML(我喜欢 YAML::Syck),然后在 Ruby 端以 YAML 形式读取数据。这样您就不需要进行评估。

On the Perl side you can output your data structures to YAML (I like YAML::Syck for this), and then read the data in as YAML on the Ruby side. That way you won't need to do an eval.

べ映画 2024-09-17 17:58:31

如果您无法更改源应用程序以输出 YAML,请使用 Kernel#负载

require 'yaml'

load 'dumped_file', true
puts $VAR1.to_yaml

If for you're unable to change the source application to output YAML, use Kernel#load:

require 'yaml'

load 'dumped_file', true
puts $VAR1.to_yaml
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文