gpt4 book ai didi

Perl YAML::Load 无法加载 YAML::Dump 转储的 YAML

转载 作者:行者123 更新时间:2023-12-02 09:23:29 30 4
gpt4 key购买 nike

我在 YAML 中遇到这个问题通过 perl 使用时。谁能告诉我哪里出了问题。

我有一个代码片段

use YAML;
...
my $ifdef_struct = YAML::Load(<<'DS_TEMPLATE');
---
'<define_name>': undef
DS_TEMPLATE
my @tmp;
push(@tmp, $ifdef_struct);
$ifdef_struct = \@tmp;
print YAML::Dump($ifdef_struct);

这会转储

    ---
- '<define_name>': undef

现在,当我将代码更改为与 YAML::Dump 转储的格式相同时

use YAML;
...
my $ifdef_struct = YAML::Load(<<'DS_TEMPLATE');
---
- '<define_name>': undef
DS_TEMPLATE
my @tmp;
push(@tmp, $ifdef_struct);
# $ifdef_struct = \@tmp;
print YAML::Dump($ifdef_struct);

它无法加载它并给出错误

Uncaught exception from user code:
YAML Error: Couldn't parse single line value
Code: YAML_PARSE_ERR_SINGLE_LINE
Line: 2
Document: 1

欢迎任何建议。

最佳答案

YAML(模块)期望的格式是:

---
-
'<define_name>': undef

但是,

---
- '<define_name>': undef

是有效的 YAML(格式)。如果您阅读 YAML 的文档,您会发现以下警告:

If you want robust and fast YAML processing using the normal Dump/Load API, please consider switching to YAML::XS. It is by far the best Perl module for YAML at this time. It requires that you have a C compiler, since it is written in C.

YAML::XS这两个版本的 YAML 都没有问题:

#!/usr/bin/perl

use strict;
use YAML::XS;
use Data::Dumper;
use warnings;

my $one_line = YAML::XS::Load(<<'EOS');
---
- '<define_name>': undef
EOS

my $multi_line = YAML::XS::Load(<<'EOS');
---
-
'<define_name>': undef
EOS

print Dumper($one_line, $multi_line);

输出:

$VAR1 = [
{
'<define_name>' => 'undef'
}
];
$VAR2 = [
{
'<define_name>' => 'undef'
}
];

关于Perl YAML::Load 无法加载 YAML::Dump 转储的 YAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40022789/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com