Perl:YAML在数组中迭代?

pac*_*acv 3 perl yaml

我正在关注此示例在Perl脚本中使用YAML配置文件中的数据的简单示例

vihtorr @ w00w/var/www $ cat test.yaml

IPs: [500, 600, 200, 100]
Run Code Online (Sandbox Code Playgroud)

vihtorr @ w00w/var/www $ cat yam2.pl

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

 my $settings = LoadFile('test.yaml');
 print "The IPs are ", $settings->{IPs};
Run Code Online (Sandbox Code Playgroud)

我想知道谁在数组内迭代?

当我执行我得到的代码

perl yam2.pl 
The IPs are ARRAY(0x166e5e0)
Run Code Online (Sandbox Code Playgroud)

谢谢你帮助一个菜鸟

ike*_*ami 9

$settings->{IPs}
Run Code Online (Sandbox Code Playgroud)

拥有对数组的引用.数组被解除引用

@{ $ref }       # The whole thing
${ $ref }[$i]   # One element
$ref->[$i]      # One element
@{ $ref }[@i]   # Array slice
Run Code Online (Sandbox Code Playgroud)

所以你可以使用

@{ $settings->{IPs} }
Run Code Online (Sandbox Code Playgroud)

你得到:

print "The IPs are ", join(', ', @{ $settings->{IPs} }), "\n";
Run Code Online (Sandbox Code Playgroud)

你可能也会分道扬..

for my $ip (@{ $settings->{IPs} }) {
   ... do something with $ip ...
}
Run Code Online (Sandbox Code Playgroud)

参考文献: