我正在从YAML
文件中读取一些信息
groceries.yaml
# comment
fruit:
apples: 1
oranges: 1
grapes: 1
vegetables:
potatoes: 1
onions: 1
leeks: 1
成perl
脚本
myscript.pl
#!/usr/bin/perl
use strict;
use warnings;
use YAML::Tiny;
# Create a YAML file
my $stuff = YAML::Tiny->new;
# Open the config
$stuff = YAML::Tiny->read( 'groceries.yaml' );
print "Fruit: ", %{($stuff->[0]->{fruit})},"\n";
print "Vegetables: ", %{($stuff->[0]->{vegetables})},"\n";
exit
这很好用,但我想有一个哈希值fruit
和一个vegetables
. 我天真的尝试是
my @keys = keys %{($stuff->[0])};
foreach my $key (@keys){
my %{ $key } = %{($stuff->[0]->{$key})},"\n";
}
但显然这不起作用。
我很想了解我做错了什么,并对实现相同想法的不同工作流程持开放态度:)