【发布时间】:2014-10-27 07:47:39
【问题描述】:
我想要做的应该非常简单明了。
use JSON;
use YAML;
use Data::Dumper;
my $yaml_hash = YAML::LoadFile("data_file.yaml");
print ref($yaml_hash) # prints HASH as expected
print Dumper($yaml_hash) # correctly prints the hash
my $json_text = encode_json($yaml_hash);
encode_json 错误提示:
cannot encode reference to scalar 'SCALAR(0x100ab630)' unless the scalar is 0 or 1
我无法理解为什么 encode_json 认为 $yaml_hash 是对标量的引用,而实际上它是对 HASH 的引用
我做错了什么?
【问题讨论】:
-
请注意,JSON 是 YAML 的 子集,如果数据仅使用 JSON 支持的功能,则无需转换。另一方面,如果数据确实使用了 YAML 特有的某些功能,则无法进行翻译。