Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Perl访问xml数组?_Xml_Perl - Fatal编程技术网

如何从Perl访问xml数组?

如何从Perl访问xml数组?,xml,perl,Xml,Perl,我有一个perl脚本,它连接到我的Icecast2服务器并监视用户列表。我试图以数组的形式访问侦听器列表,以便为每个侦听器执行一个操作 相关子程序: sub process_list_clients() { my ($mount) = @_; my $icecast_mount = Net::Icecast2::Mount->new( host => $ic_host, port => $ic_po

我有一个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'
          }
        ];
子例程的输出:

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>

7.
蒙面
自由开关(mod_shout)/1.0
798
233307
蒙面
自由开关(mod_shout)/1.0
715
233336
蒙面
自由开关(mod_shout)/1.0
596
233370
蒙面
自由开关(mod_shout)/1.0
526
233392
蒙面
自由开关(mod_shout)/1.0
440
233419
蒙面
自由开关(mod_shout)/1.0
368
233445
蒙面
自由开关(mod_shout)/1.0
91
233511
尝试这样做(例如IP值):

来自
Data::Dumper
$VAR1
显示您对
数组的引用。所以,当您需要使用
@{}
语法的@array时,必须取消引用它

每一个Perl程序员都应该知道的基本知识和事情是:

my $array_ref = []; # a reference to a void ARRAY
my $hash_ref = {};  # a reference to a void HASH
检查


只打印第一个侦听器的IP地址。现在呢?这是你想要的吗?谢谢,这正是我想要的。它现在打印出一个IP地址列表。这个答案非常有效。我的问题可能值得投反对票,但不是这个答案。