1

I have a perl script that connects to my Icecast2 server and monitors the list of users. I'm trying to access the list of listeners as an array so that I can perform an action for each listener

Relevant Subroutine:

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);
        }
}

Output from the subroutine:

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'
          }
        ];

Here is the xml output from Icecast2:

<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>

1 Answer 1

3

Try doing this (example is for IP value) :

my @listeners = @{ $xml->{source}->{listener} };
foreach my $listener (@listeners) {
    print "$listener->{IP}\n";
}

The $VAR1 from Data::Dumper show that you have a reference to an ARRAY. So you have to de-reference it when you want an @array with the @{ } syntax.

The basic and the thing that every Perl coder should know :

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

Check
http://perldoc.perl.org/perlref.html
http://perldoc.perl.org/perlreftut.html

Sign up to request clarification or add additional context in comments.

4 Comments

That prints the IP address off of the first listener only.
And now ? Is it what you expected ?
Thanks, that's exactly what I was looking for. It now prints off a list of IP addresses.
This answer works perfectly. My question might deserve a downvote but not this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.