Asterisk cmd Perl
Created by: JustRumours,Last modification on Fri 12 of Oct, 2007 [11:48 UTC] by chandave
res_perl
Asterisk — A telephony toolkit for Linux.
res_perl — A Module To Incorporate Perl Into Asterisk.
Copyright (C) 2005, Anthony Minessale
Anthony Minessale <anthmct@yahoo.com>
res_perl 3.5 avaliable from PBXFreeware.org
DESCRIPTION:
This is res_perl the "mod_perl" of sorts for Asterisk. res_perl uses Perl embedding to allow a Perlenvironment to be created inside a runnning Asterisk. An Asterisk-specific perl module AstAPI.pm is loaded in
this environment so when Perl code is executing, it has full access to a large asterisk API layer.
IMPORTANT NOTE:
As of this version you need to have perl compiled with usethreads and useithreadsFor a quick build of perl from src use 'make perl' *NOTE* ***this will erase your current perl***
FEATURES:
res_perl can execute perl code from extensions.conf in a special package called Asterisk::Embedthat is loaded on startup from /usr/local/res_perl/modules/asterisk_init.pm the first arguement to the
Perl app is the name of a subroutine from this Asterisk::Embed package followed by a : seperated list
of optional parameters.
EX: exten => 1,1,Perl(myfunc:arg_1:arg_2:arg_n......)
*NOTE* When you execute the Perl() app your Perl Environment will be cloned for that execution.
Any changes you make to the environment during the execution will be discarded when the execution ends.
This is a limitation of embeded thread support in Perl.
If you change asterisk_init.pm you need to enter the cli command "perl labotomy" to read the changes.
(*NOTE* labotomy not recommended in production!)
There is also a module called LoadFile that allows you to store your perl apps
in a seperate file and load them into memory without restarting. Files are loaded from
/usr/local/res_perl/apps automaticly.
EX: exten => 1,1,Perl(Loadfile:demo.pl:arg_1:arg_2:arg_n....)
If you alter the file demo.pl it will then the next execution will load it from disk instead of memory.
To re-cache just send the cli command "perl call LoadFileCacheAll". See /usr/local/res_perl/apps/demo.pl
Inside these functions you have access to several perlified api commands starting with asterisk_
see /usr/local/res_perl/modules/asterisk_init.pm for a few examples
(see AstAPIBase.c for the C defs they are all valid from perl once you are up and running.)
SPECIAL RETURN VALUES:
There are also *some* special return commands: (1 for now)thread:<function>
This will spin off a thread with the perl sub you specify running inside it.
the sub will get 1 scalar arg being the number of times it was run (starting with 1)
STARTUP:
The function startup() is called from asterisk_init.pm when the module is loaded.FUNCTIONS:
res_perl also registers the PERL function to the dialplan. Unlike the Perl() app the PERL functionexecutes in the Main environment and therefore can be used to make permenant changes to the environment.
A lock is held during the execution of the PERL function so a prolonged set of instructions is not advised.
The PERL function takes it's arguements the same way as the Perl() application does.
EX: exten => 1,1,Dial(${PERL(LoadFile:find_dest:${EXTENSION})})
CONFIG:
res_perl has hooks into both the asterisk runtime loadable configuration engine (res_config) and realtime.There is a module called AstConfig.pm that lets you create, load and save files in the asterisk config format.
AstConfig.pm has a special method return_data() that returns a special Perl-to-Asterisk version of the config
object that can be passed to Asterisk. (See sub perl_config() in /usr/local/res_perl/modules/asterisk_init.pm)
SWITCH:
res_perl exposes the Asterisk Switch API to a Perl equivilant set of calls making it possible to create yourown dialplan switch with Perl code. See sub perl_switch_register in /usr/local/res_perl/modules/asterisk_init.pm
CDR:
res_perl exposes a hook to the Asterisk CDR handler. sub perl_log is called which will be called with a hash of all thecdr info as it's arguement. Also, the special HASHES %CHAN_VARS and %CDR_VARS will already exist in the fashion of %ENV
Asterisk | Applications | Functions | Variables | Expressions | Asterisk FAQ

Comments
333Return value from Perl to Asterisk
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.
I have written code like this.
exten => 9999,23,AGI(insert_into_database.pl|${intime}|${outtime}|${telephone_number}|${date}|${month}|${year}
in Perl Script:
$fullDate = $ARGV3."".SARGV4."".$ARGV5;
...........
........(some more code)
if($record == 0 && $rec == 0 && $pri == 0)
{
$stmt = "INSERT into $table_name values($ARGV0,$ARGV1,$ARGV2,$fullDate)";
$fetch = $dbh->prepare($stmt);
$fetch->execute();
}
else
{
print "\nValues cannot be inserted in database\n\n";
}
If that "if" condition satisfies, then it has to return some value, say 0.Otherwise 1. Even if i write return 0, there in "if" condition, how can i get that value to asterisk?
Pls suggest me how to proceed.
Thanks in advance,
Padmaja T N.
333res_perl with asterisk 1.4 !!
now we have the same problem of res_perl compilation with asterisk 1.4. It is the same problem that was present when asterisk was upgraded to version 1.2.
I hope Anthony Minessale will be able to solve that problem as he did on that case.
Here is the same problem again:
gcc -Wall -DRES_PERL_BASE="\"/usr/local/res_perl\"" -DMULTIPLICITY -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm -I/usr/local/lib/perl5/5.8.8/i686-linux-thread-multi/CORE -I/usr/src/asterisk -I/usr/src/asterisk/include -I. -c res_perl.c
In file included from ./res_perl.h:17,
from res_perl.c:17:
/usr/src/asterisk/include/asterisk/module.h:204: warning: "struct ast_channel" declared inside parameter list
/usr/src/asterisk/include/asterisk/module.h:204: warning: its scope is only this definition or declaration, which is probably not what you want
In file included from ./res_perl.h:22,
from res_perl.c:17:
/usr/local/lib/perl5/5.8.8/i686-linux-thread-multi/CORE/perl.h:2424: error: syntax error before "perl_mutex"
/usr/local/lib/perl5/5.8.8/i686-linux-thread-multi/CORE/perl.h:2424: warning: type defaults to `int' in declaration of `perl_mutex'
/usr/local/lib/perl5/5.8.8/i686-linux-thread-multi/CORE/perl.h:2424: warning: data definition has no type or storage class
/usr/local/lib/perl5/5.8.8/i686-linux-thread-multi/CORE/perl.h:2425: error: syntax error before "perl_cond"
/usr/local/lib/perl5/5.8.8/i686-linux-thread-multi/CORE/perl.h:2425: warning: type defaults to `int' in declaration of `perl_cond'
/usr/local/lib/perl5/5.8.8/i686-linux-thread-multi/CORE/perl.h:2425: warning: data definition has no type or storage class
In file included from /usr/local/lib/perl5/5.8.8/i686-linux-thread-multi/CORE/perl.h:3988,
from ./res_perl.h:22,
from res_perl.c:17:
/usr/local/lib/perl5/5.8.8/i686-linux-thread-multi/CORE/perlvars.h:48: error: syntax error before "PL_op_mutex"
/usr/local/lib/perl5/5.8.8/i686-linux-thread-multi/CORE/perlvars.h:48: warning: type defaults to `int' in declaration of `PL_op_mutex'
/usr/local/lib/perl5/5.8.8/i686-linux-thread-multi/CORE/perlvars.h:48: warning: data definition has no type or storage class
/usr/local/lib/perl5/5.8.8/i686-linux-thread-multi/CORE/perlvars.h:52: error: syntax error before "PL_dollarzero_mutex"
/usr/local/lib/perl5/5.8.8/i686-linux-thread-multi/CORE/perlvars.h:52: warning: type defaults to `int' in declaration of `PL_dollarzero_mutex'
/usr/local/lib/perl5/5.8.8/i686-linux-thread-multi/CORE/perlvars.h:52: warning: data definition has no type or storage class
res_perl.c:492: warning: initialization from incompatible pointer type
res_perl.c: In function `_update_perl':
res_perl.c:837: warning: ISO C90 forbids mixed declarations and code
res_perl.c: At top level:
res_perl.c:921: warning: initialization from incompatible pointer type
res_perl.c:1023: warning: initialization from incompatible pointer type
res_perl.c: In function `_load_module':
res_perl.c:1036: warning: ISO C90 forbids mixed declarations and code
make: *** res_perl.o Error 1
333Memory leak in res_perl
I'm using res_perl with asterisk 1.0.0. And after running asterisk a couply of months, I see that the process asterisk take a lot on memory.
And asterisk will freeze.
If I look in the logging I see that the last command asterisk perfomed is a call to a perl program.
So I think that there is a memory link in res_perl.
Does anybody know if this is the case and maybe knows a sollution to this problem?
Kind regards,
333
I added:
load => res_features.so
to /etc/asterisk/modules.conf just before the globals section and which at least enabled Asterisk to start with res_perl. I starts up, and seems to work, but I haven't tested any features yet.
Also, it looks like an auto-generated file (AstAPIBase_wrap.c) is outdated, so asterisk will crash with the demo.pl program. To fix run:
make swigclean
make AstAPI_wrap.c
before building res_perl.
This is with Asterisk 1.2.1 on Centos 4.2 (i.e. Asterisk@Home )
333Re: make res_perl with asterisk 1.2
Thanks to Anthony Minessale now the new code compiles with no error at all, you need to download it again from PBXFreeware.org.
But when I try to start asterisk.12 it gives the following error:
res_perl.soDec 4 21:14:03 WARNING20942: loader.c:325 __load_resource: /usr/lib/asterisk/modules/res_perl.so: undefined symbol: ast_bridge_call
Dec 4 21:14:03 WARNING20942: loader.c:554 load_modules: Loading module res_perl.so failed!
Is this a res_perl problem or is it just my asterisk problem??
333Re: make res_perl with asterisk 1.2
Thanks to Anthony Minessale now the new code compiles with no error at all, you need to download it again from PBXFreeware.org.
But when I try to start asterisk.12 it gives the following error:
res_perl.soDec 4 21:14:03 WARNING20942: loader.c:325 __load_resource: /usr/lib/asterisk/modules/res_perl.so: undefined symbol: ast_bridge_call
Dec 4 21:14:03 WARNING20942: loader.c:554 load_modules: Loading module res_perl.so failed!
Is this a res_perl problem or is it just my asterisk problem??
333Re: make res_perl with asterisk 1.2
my system is FC4, Asterisk 1.2 stable and Res_PERL 3.5 and Perl 5.8.7 with multithreads.
Anyone with success?
333make res_perl with asterisk 1.2
/usr/src/asterisk/contrib/scripts/astxs -nolink -append=CFLAGS:"-DRES_PERL_BASE="'\"/usr/local/res_perl\"'" -DMULTIPLICITY -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE -I/usr/src/asterisk -I/usr/src/asterisk/include -I." res_perl.c
gcc -I/usr/src/asterisk -I/usr/src/asterisk/include -pipe -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -g3 -Iinclude -I../include -D_REENTRANT -D_GNU_SOURCE -O6 -march=i686 -fomit-frame-pointer -fPIC -DRES_PERL_BASE=\"/usr/local/res_perl\" -DMULTIPLICITY -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE -I/usr/src/asterisk -I/usr/src/asterisk/include -I. -c res_perl.c -o res_perl.o
In file included from res_perl.h:55,
from res_perl.c:17:
/usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE/perl.h:2075: error: syntax error before "perl_mutex"
/usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE/perl.h:2075: warning: type defaults to `int' in declaration of `perl_mutex'
/usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE/perl.h:2075: warning: data definition has no type or storage class
/usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE/perl.h:2076: error: syntax error before "perl_cond"
/usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE/perl.h:2076: warning: type defaults to `int' in declaration of `perl_cond'
/usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE/perl.h:2076: warning: data definition has no type or storage class
In file included from /usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE/perl.h:3595,
from res_perl.h:55,
from res_perl.c:17:
/usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE/perlvars.h:48: error: syntax error before "PL_op_mutex"
/usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE/perlvars.h:48: warning: type defaults to `int' in declaration of `PL_op_mutex'
/usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE/perlvars.h:48: warning: data definition has no type or storage class
/usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE/perlvars.h:52: error: syntax error before "PL_dollarzero_mutex"
/usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE/perlvars.h:52: warning: type defaults to `int' in declaration of `PL_dollarzero_mutex'
/usr/lib/perl5/5.8.6/i686-linux-thread-multi/CORE/perlvars.h:52: warning: data definition has no type or storage class
make: *** res_perl.o Error 255
Can you tell me where is the problem . I can not compile res_perl with new asterisk 1.2. May by is not define perl_mutex and perl_cond type but where must be defined i dont now. :(
333Makefile problem
perl -MExtUtils::Embed -e xsinit
That command should probably be added to the Makefile patch here:
- res_perl
perlxsi.o:perl -MExtUtils::Embed -e xsinit
$(CC) -c perlxsi.c $(PERLCFLAGS) -o perlxsi.o
or something like this:
perlxsi.o: perlxsi.c
$(CC) -c perlxsi.c $(PERLCFLAGS) -o perlxsi.o
perlxsi.c:
perl -MExtUtils::Embed -e xsinit
333I found this
is that correct?