处理 YAML 中的尾随冒号
我正在使用 YAML::Tiny 在 Perl 中写入和读取文件。不幸的是,我遇到了以冒号结尾的数据问题:
my $test_message = {"hoody" => 'hoo:'};
my $dump = YAML::Tiny::Dump($test_message);
my $reloaded = YAML::Tiny::Load($dump);
print Data::Dumper::Dumper($reloaded);
这会产生 Failed to load YAML document from string at [the line with YAML::Tiny::Load]
。
首先,这看起来像是一个错误。是吗?或者我对 YAML::Tiny 的期望太高了? (我们使用的是 YAML::Tiny 1.48;最新的是 1.50,IT 部门向我保证很快就会安装它。)
其次,有什么方法可以解决这个问题吗?我正在尝试使用转义字符,但我一定做得不对。我尝试用 \x3A
替换冒号,但这并没有给出所需的行为。 (打印 $dump
会返回未更改的字符串 [hoo\x3A
], 而 Data::Dumper::Dumper
行使其成为 'hoo\\x3A'
。)
提前感谢您对此提供的任何帮助。
I'm using YAML::Tiny to write to and read from a file in Perl. Unfortunately, I'm having a problem with data that ends with a colon:
my $test_message = {"hoody" => 'hoo:'};
my $dump = YAML::Tiny::Dump($test_message);
my $reloaded = YAML::Tiny::Load($dump);
print Data::Dumper::Dumper($reloaded);
This produces Failed to load YAML document from string at [the line with YAML::Tiny::Load]
.
First off, this looks like a bug. Is it? Or am I expecting too much from YAML::Tiny? (We're using YAML::Tiny 1.48; the newest is 1.50, which IT assures me will be installed soon.)
Second, is there any way to work around this? I'm trying to use escape characters, but I must not be doing it right. I tried replacing the colon with \x3A
, but that doesn't give the desired behavior. (Printing $dump
gives the string back unchanged [hoo\x3A
],
while the Data::Dumper::Dumper
line makes it 'hoo\\x3A'
.)
Thanks in advance for any help on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此错误已在 1.5 中修复
您可以考虑使用 YAML 而不是 YAML::Tiny (Tiny 版本应该与其父版本具有相同的接口)。
我认为,在您最终获得带有错误修复的 YAML::Tiny 1.5 后,任何解决方法只会让事情变得更糟。我建议您在代码中使用
use YAML::Tiny 1.50
或切换到 YAML。This bug was fixed in 1.5
You may consider using YAML instead of YAML::Tiny (Tiny version should have just the same interface as it's parent).
I think any workarounds will make things only worsen after you finally get YAML::Tiny 1.5 with bugfixes. I'd recommend you put
use YAML::Tiny 1.50
in your code or switch to YAML.