Return values from Perl to asterisk

Hi all,
I’m stuck with how to pass the return value to asterisk. That is, a value has to be passed from perl script to asterisk. The variable returned in Perl has to be passed to asterisk, where if the return value is 1 from perl , i have to do something and if return value is 0, i have to do something else.
Pls suggest me how to proceed.:cry:

Thanks in advance,
Padmaja T N.

In the perl script just set an * dialplan variable with the commad “SET VARIABLE myvar myvalue”, then in the dialplan you can check the value of the variable myvar.

Check for more informations.


Marco Bruni

Thanks a lot for responding. I have tried that also. But could not find any solution. Pls suggest me how to proceed further.

Thanks in advance,
Padmaja T N.

Are you using AGI to call the perl program, or are you using System? Marco already provided the solution if you are using AGI. If you are using System look here:

Thanks for responding.This is the code what i have written.

if($record == 0 && $rec == 0 && $pri == 0)
$stmt = "INSERT into $table_name values($ARGV[0],$ARGV[1],$ARGV[2],$fullDate)";
$fetch = $dbh->prepare($stmt);


print "\nValues cannot be inserted in database\n\n";




I need to pass that [color=red]$value[/color] to the asterisk extensions.conf
Because, if the values are not inserted in database, i need to repeat the process of again accepting the values from the user.
Kindly suggest me how to proceed.

Thanks in advance,
Padmaja T N.

At the top of your script insert this lines:

use Asterisk::AGI;

# the AGI object
my $agi = new Asterisk::AGI;

Then just before the exit perl function insert:

$agi->set_variable('value', $value);

Now in the dialplan you can check the variable “value” with Gotoif.

The class Asterisk::AGI is defined in the module asterisk-perl, you can install it from the cpan site, is the home page.

Hope it helps.


Marco Bruni

Better is to put

$agi->set_variable('value', "\"$value\"");

This is protection in case $value is “” .

Thank You sooooooooo much friends. Its working. :smiley:

I’ve copied and pasted my code below so you guys can check it out. Its very straightforward, however when I just simply pass back the value I got from asterisk from within perl, nothing gets sent back.

; inside .conf file

exten => 333,1,Answer()
exten => 333,2,Read(zipcode|beep|5||2|5)
exten => 333,3,Read(last4cc|beep|4||2|5)
exten => 333,4,AGI(/var/lib/asterisk/agi-bin/searchmembership.agi|${zipcode}|${last4cc})
exten => 333,5,NoOp(${iUID})
exten => 333,6,Chanspy(SIP/$(SIP))
exten => 333,7,hangup


inside .agi file

#!/usr/bin/perl -w

don’t cache variables


use Asterisk::AGI;

initialize the AGI module and read incoming arguments

my $AGI = new Asterisk::AGI;
my (@return);

retrieve variables

my %input = $AGI->ReadParse();

my ($sZipCode, $sLast4cc);
$sZipCode = $ARGV[0];
$sLast4cc = $ARGV[1];

print stored values

$AGI->verbose(“ZIPCODE: $sZipCode”);
$AGI->verbose(“LAST4CC: $sLast4cc”);

return values to asterisk




You see I’m just merely passing back the value of “iUID” to asterisk from inside the AGI/perl script using set_variable but when I use the “NoOp” function to check for the value to see if its working but nothing is displayed.

Is there anything else causing this to happen?? PLEASE HELP!