我有一个linux命令,它从api中以yaml格式获取一些数据。我一直在寻找一种方法来将此变量填充到哈希数组中。我正在尝试YAML :: Tiny,我编写的代码块是
use YAML::Tiny;
my $stuff = `unix command that returns as yaml output`
my $yaml = YAML::Tiny->new;
$yaml = YAML::Tiny->new->read_string->($stuff);
但使用此代码运行会出错
Can't use string ("") as a subroutine ref while "strict refs" in use
$ stuff变量看起来像
Cluster1:
source_mount: /mnt/uploads
dir: /a /b
default: yes
destination_mount: /var/pub
Cluster2:
source_mount: /mnt/uploads
dir: /c /d /e
default: no
destination_mount: /var/pub
答案 0 :(得分:5)
您不应该使用new
两次,->
之后不应该read_string
(请参阅YAML::Tiny的POD)。变化:
$yaml = YAML::Tiny->new->read_string->($stuff);
为:
$yaml = YAML::Tiny->read_string($stuff);
这是一个完整的工作示例:
use warnings;
use strict;
use YAML::Tiny;
my $stuff = '
Cluster1:
source_mount: /mnt/uploads
dir: /a /b
default: yes
destination_mount: /var/pub
Cluster2:
source_mount: /mnt/uploads
dir: /c /d /e
default: no
destination_mount: /var/pub
';
my $yaml = YAML::Tiny->new();
$yaml = YAML::Tiny->read_string($stuff);
print $yaml->[0]->{Cluster2}->{dir}, "\n";
__END__
/c /d /e