functions.pl 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. use Kamailio qw ( log );
  2. use Kamailio::Constants;
  3. #This function demonstrates how to call functions that are exported by other modules.
  4. sub exportedfuncs {
  5. my $m = shift;
  6. my $res = -1;
  7. if (($m->getMethod() eq "INVITE") || ($m->getMethod eq "CANCEL")) {
  8. $m->moduleFunction("xlog", "L_INFO", "x foobar");
  9. if ($m->getRURI() =~ m/sip:555[0-9]+@/) {
  10. $m->moduleFunction("sl_send_reply", "500", "Error: 555 not available");
  11. } else {
  12. $res = $m->moduleFunction("alias_db_lookup", "dbaliases");
  13. }
  14. }
  15. return $res;
  16. }
  17. # This demonstrates that a parameter may be passed to a function
  18. sub paramfunc {
  19. my $m = shift;
  20. my $param = shift;
  21. log(L_INFO, "This function was called with a parameter: $param\n");
  22. $param =~ s/l/L/g;
  23. log(L_INFO, "We can fiddle with it: $param\n");
  24. return 1;
  25. }
  26. # The following function shows that you can use exported functions just as if they were "real".
  27. # This is achieved through Perl's autoloading mechanisms.
  28. sub autotest {
  29. my $m = shift;
  30. $m->xlog("L_ERR", "This logging is done via perl's autoload mechanism and the xlog function");
  31. return 1;
  32. }
  33. # The following two functions demonstrate that the Kamailio perl module handles
  34. # dieing interpreters correctly. Kamailio itself will not crash.
  35. sub diefunc1 {
  36. my $m = shift;
  37. warn("I'll die in a moment...");
  38. log(L_INFO, "About to die...");
  39. die("Here I die!");
  40. return 1;
  41. }
  42. sub diefunc2 {
  43. my $m = shift;
  44. NoSuchClass::NoSuchMethod(noSuchParameter);
  45. return 1;
  46. }