如何编写初始YAML文件?

发布于 2024-11-03 10:14:58 字数 971 浏览 0 评论 0原文

我有一个哈希%h,我想将其保存为 YAML。

#!/usr/bin/perl

use warnings;
use strict;

my %h = ();

# -----

use YAML::Syck;
my $y = YAML::Syck::LoadFile('have_seen.yaml');

$y->%h ???

my $yaml = YAML::Syck::Dump($y);
$yaml::Syck::ImplicitUnicode = 1;

open F, ">have_seen.yaml" or die $!;
print F $yaml . "---\n";
close F;

但这似乎是一个先有鸡还是先有蛋的问题。

第一次写入yaml文件如何才能被读取?

更新:根据接受的答案是解决方案

#!/usr/bin/perl

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

my $first_time = 1;

if ($first_time) {

    my %h = ("1" => 2);

    open F, '>', 'seen.yaml';
    print F YAML::Syck::Dump(\%h);
    close F;

} else {

    my $h = YAML::Syck::LoadFile('seen.yaml');

    $h->{"3"} = 4;

    print Dumper $h;

    my $yaml = YAML::Syck::Dump($h);
    $yaml::Syck::ImplicitUnicode = 1;

    open F, ">seen.yaml" or die $!;
    print F $yaml . "---\n";
    close F;
}

I have a hash %h which I would like to save as YAML.

#!/usr/bin/perl

use warnings;
use strict;

my %h = ();

# -----

use YAML::Syck;
my $y = YAML::Syck::LoadFile('have_seen.yaml');

$y->%h ???

my $yaml = YAML::Syck::Dump($y);
$yaml::Syck::ImplicitUnicode = 1;

open F, ">have_seen.yaml" or die $!;
print F $yaml . "---\n";
close F;

But it seams like a chicken and egg problem.

How do I write the yaml file for the first time, so it can be read?

Update: Based on the accepted answer was the solution

#!/usr/bin/perl

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

my $first_time = 1;

if ($first_time) {

    my %h = ("1" => 2);

    open F, '>', 'seen.yaml';
    print F YAML::Syck::Dump(\%h);
    close F;

} else {

    my $h = YAML::Syck::LoadFile('seen.yaml');

    $h->{"3"} = 4;

    print Dumper $h;

    my $yaml = YAML::Syck::Dump($h);
    $yaml::Syck::ImplicitUnicode = 1;

    open F, ">seen.yaml" or die $!;
    print F $yaml . "---\n";
    close F;
}

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

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

发布评论

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

评论(3

反差帅 2024-11-10 10:14:58

使用DumpFile安装YAML::XS。据我所知,YAML::Syck 被视为已弃用/不受支持。

~>perl -MYAML=DumpFile -le 'DumpFile("test.yml", { o => "hai" })'
~>cat test.yml
---
o: hai

Use DumpFile, and install YAML::XS. YAML::Syck is, as far as I know, considered deprecated/unsupported.

~>perl -MYAML=DumpFile -le 'DumpFile("test.yml", { o => "hai" })'
~>cat test.yml
---
o: hai
独孤求败 2024-11-10 10:14:58

使用 Dump 将任意对象转换为 YAML 编码的字符串,然后将该字符串打印到文件中。

$h{foo} = "bar";
$h{"answer to life, the universe, and everything"} = 42;

open F, '>', 'have_seen.yaml';
print F YAML::Syck::Dump( \%h );
close F;

Use Dump to convert an arbitrary object to a YAML-encoded string, then print that string to a file.

$h{foo} = "bar";
$h{"answer to life, the universe, and everything"} = 42;

open F, '>', 'have_seen.yaml';
print F YAML::Syck::Dump( \%h );
close F;
踏雪无痕 2024-11-10 10:14:58
#!/usr/bin/perl
use warnings;
use strict;

use YAML::Any qw'DumpFile LoadFile';

my $data_filename = 'seen.yaml';

my $data = LoadFile( $data_filename );

unless( $data ){
  # first time
  $data = {
    1 => 2
  };
}

$data->{3} = 4;

DumpFile( $data_filename, $h );
#!/usr/bin/perl
use warnings;
use strict;

use YAML::Any qw'DumpFile LoadFile';

my $data_filename = 'seen.yaml';

my $data = LoadFile( $data_filename );

unless( $data ){
  # first time
  $data = {
    1 => 2
  };
}

$data->{3} = 4;

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