0

i'm trying to use the Asterisk::AMI package but with a simple example dont works

#!/usr/bin/perl -w
# ami_test.pl

use strict;
use diagnostics;
use Asterisk::AMI;

// use default 127.0.0.1 5038
my $astman = Asterisk::AMI->new(
    Username => 'manager',
    Secret => 'secret'
);

die "Unable to connect to Asterisk" unless ($astman);

my $response = $astman->({
    Action => 'Command',
    Command => 'sip show peers'
});


print $response->{'Response'};

Alway i get an error :

Not a CODE reference at ami_test.pl line 17 (#1)
    (F) Perl was trying to evaluate a reference to a code value (that is, a
    subroutine), but found a reference to something else instead.  You can
    use the ref() function to find out what kind of ref it really was.  See
    also perlref.

Uncaught exception from user code:
        Not a CODE reference at ami_test.pl line 17.
 at ami_test.pl line 17

EDIT

the documentation see wrong, i've try

my $response = $astman->action({
    Action => 'Command',
    Command => 'sip show peers'
});

and works fine

rkmax
  • 17,633
  • 23
  • 91
  • 176

2 Answers2

2

The documentation for Asterisk::AMI is wrong. You should write

my $response = $astman->action({
    Action => 'Command',
    Command => 'sip show peers'
});

which is equivalent to

my $action = $astman->send_action({
    Action => 'Command',
    Command => 'sip show peers'
});

my $response = $astman->get_response($action);

By default there is no timeout on actions. To specify a default timeout for all actions, create your AMI object using, for example

my $astman = Asterisk::AMI->new(
    Username => 'manager',
    Secret => 'secret',
    Timeout => 10
);
Borodin
  • 126,100
  • 9
  • 70
  • 144
2

You have the astman. If you do this:

$astman->({ Action => 'Ping' }, \&actioncb);

You are passing parameters to an object.

You should pass parameters to a method, for example:

my $action = $astman->send_action({ Action => 'Ping' }, \&actioncb);

The Perl Documentation is fine. The CPAN web site for Asterisk::AMI has a little mistake (at least in version 0.2.8).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Raul
  • 21
  • 1
  • Welcome to Stack Overflow. I'm not sure whether 'astman' is a bit of jargon for users of Asterisk or your own contraction. When you reference a module on CPAN, use the 'permalink' (top right, just below the user's icon) as the permanent reference which moves to the current version automatically. I'm not sure that your answer provides information that was not already given by others, which makes it of limited value, even when it is correct. – Jonathan Leffler Sep 25 '12 at 02:45