我有一个perl脚本连接到我的Icecast2服务器并监视用户列表。我试图以数组的形式访问侦听器列表,以便我可以为每个侦听器执行操作
相关子程序:
sub process_list_clients()
{
my ($mount) = @_;
my $icecast_mount = Net::Icecast2::Mount->new(
host => $ic_host,
port => $ic_port,
protocol => $ic_protocol,
login => $ic_login,
password => $ic_password,
mount => $mount
);
my $xml = $icecast_mount->list_clients;
print STDERR Dumper($xml);
print STDERR "Listeners: " . $xml->{source}->{Listeners} . "\n";
print STDERR "Mount: " . $xml->{source}->{mount} . "\n";
my @listeners = $xml->{source}->{listener};
foreach my $listener (@listeners)
{
print STDERR Dumper($listener);
}
}
子程序的输出:
Listeners: 6
Mount: /masked
$VAR1 = [
{
'ID' => '233307',
'Connected' => '705',
'IP' => 'masked',
'UserAgent' => 'FreeSWITCH(mod_shout)/1.0'
},
{
'ID' => '233336',
'Connected' => '622',
'IP' => 'masked',
'UserAgent' => 'FreeSWITCH(mod_shout)/1.0'
},
{
'ID' => '233370',
'Connected' => '503',
'IP' => 'masked',
'UserAgent' => 'FreeSWITCH(mod_shout)/1.0'
},
{
'ID' => '233392',
'Connected' => '433',
'IP' => 'masked',
'UserAgent' => 'FreeSWITCH(mod_shout)/1.0'
},
{
'ID' => '233419',
'Connected' => '347',
'IP' => 'masked',
'UserAgent' => 'FreeSWITCH(mod_shout)/1.0'
},
{
'ID' => '233445',
'Connected' => '275',
'IP' => 'masked',
'UserAgent' => 'FreeSWITCH(mod_shout)/1.0'
}
];
以下是Icecast2的xml输出:
<icestats>
<source mount="/masked">
<Listeners>7</Listeners>
<listener>
<IP>masked</IP>
<UserAgent>FreeSWITCH(mod_shout)/1.0</UserAgent>
<Connected>798</Connected>
<ID>233307</ID>
</listener>
<listener>
<IP>masked</IP>
<UserAgent>FreeSWITCH(mod_shout)/1.0</UserAgent>
<Connected>715</Connected>
<ID>233336</ID>
</listener>
<listener>
<IP>masked</IP>
<UserAgent>FreeSWITCH(mod_shout)/1.0</UserAgent>
<Connected>596</Connected>
<ID>233370</ID>
</listener>
<listener>
<IP>masked</IP>
<UserAgent>FreeSWITCH(mod_shout)/1.0</UserAgent>
<Connected>526</Connected>
<ID>233392</ID>
</listener>
<listener>
<IP>masked</IP>
<UserAgent>FreeSWITCH(mod_shout)/1.0</UserAgent>
<Connected>440</Connected>
<ID>233419</ID>
</listener>
<listener>
<IP>masked</IP>
<UserAgent>FreeSWITCH(mod_shout)/1.0</UserAgent>
<Connected>368</Connected>
<ID>233445</ID>
</listener>
<listener>
<IP>masked</IP>
<UserAgent>FreeSWITCH(mod_shout)/1.0</UserAgent>
<Connected>91</Connected>
<ID>233511</ID>
</listener>
</source>
</icestats>
答案 0 :(得分:3)
尝试这样做(例子是IP值):
my @listeners = @{ $xml->{source}->{listener} };
foreach my $listener (@listeners) {
print "$listener->{IP}\n";
}
来自$VAR1
的{{1}}表示您有引用到Data::Dumper
。因此,当您需要具有ARRAY
语法的@array时,必须取消引用。
每个Perl编码器应该知道的基本和事物:
@{ }
检查
http://perldoc.perl.org/perlref.html
http://perldoc.perl.org/perlreftut.html