在 Perl 脚本中使用 YAML 配置文件中的数据的简单示例

发布于 2024-09-14 10:34:27 字数 334 浏览 5 评论 0原文

我需要创建一个 YAML 文件来存储 Perl 脚本的一些配置数据。这看起来应该很容易,但我一直没能解决,我想如果我只有一个简单的例子来复制我就可以了。我想做这样的事情:

-----test.yaml-----
image_width: 500
show_values: 0
-------------------

------test.perl------
use YAML;

my (%settings) = Load('test.yaml');
print "The image width is", $settings{image_width};
---------------------

I need to create a YAML file to store some configuration data for a Perl script. This seems like it should be really easy but I haven't been able to work it out, I think if I had just one simple example to copy I'd be fine. I want to do something like this:

-----test.yaml-----
image_width: 500
show_values: 0
-------------------

------test.perl------
use YAML;

my (%settings) = Load('test.yaml');
print "The image width is", $settings{image_width};
---------------------

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

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

发布评论

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

评论(3

反差帅 2024-09-21 10:34:27

试试这个:

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use YAML qw(LoadFile);

my $settings = LoadFile('test.yaml');
say "The image width is ", $settings->{image_width};

-迈克尔

Try this:

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use YAML qw(LoadFile);

my $settings = LoadFile('test.yaml');
say "The image width is ", $settings->{image_width};

– Michael

悲歌长辞 2024-09-21 10:34:27

这里的根本问题是 Load 需要一个包含 YAML 的字符串,而不是文件名。您需要使用 LoadFile (默认情况下不会导出)。另外,您应该使用 YAML::XS 而不是 YAML 如果可以的话,因为它是一个更好的实现。 (但是 YAML 对于简单的配置文件来说应该足够了。)

另一个问题是 LoadFile 将返回哈希引用(好吧,如果您的 YAML 看起来像哈希,就像您的那样),而不是列表您可以使用它来初始化哈希。

试试这个:(

use strict;
use warnings;
use YAML::XS qw(LoadFile);

my $settings = LoadFile('test.yaml');

print "The image width is ", $settings->{image_width};

如果您不想(或不能)安装 YAML::XS,可以删除 ::XS。该程序无需其他更改即可运行。)

Your fundamental problem here is that Load expects a string containing YAML, not a filename. You wanted LoadFile instead (which is not exported by default). Also, you should use YAML::XS instead of YAML if you can, as it's a better implementation. (But YAML should be adequate for a simple config file.)

The other problem is that LoadFile will return a hash reference (well, if your YAML looks like a hash, as yours does), not a list you can use to initialize a hash.

Try this:

use strict;
use warnings;
use YAML::XS qw(LoadFile);

my $settings = LoadFile('test.yaml');

print "The image width is ", $settings->{image_width};

(You can delete the ::XS if you don't want to (or can't) install YAML::XS. The program will work with no other changes.)

忆离笙 2024-09-21 10:34:27

尝试转储您想要的配置。

use strict;
use warnings;

use YAML;

my %settings = (
        foo => 1,
        bar => [qw/one two three/],
);

print Dump(\%settings);

---
bar:
  - one
  - two
  - three
foo: 1

另外,如果规范有点过于密集,维基百科有一个很好的 YAML 概述

Try dumping out the configuration you want.

use strict;
use warnings;

use YAML;

my %settings = (
        foo => 1,
        bar => [qw/one two three/],
);

print Dump(\%settings);

This prints

---
bar:
  - one
  - two
  - three
foo: 1

Also, wikipedia has a good overview of YAML if the specification is a bit too dense.

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