Page 238 - Asterisk™: The Future of Telephony
P. 238

Writing AGI Scripts in Perl

               Asterisk comes with a sample AGI script called agi-test.agi. Let’s step through the file
               while we cover the core concepts of AGI programming. While this particular script is
               written in Perl, please remember that your own AGI programs may be written in almost
               any programming language. Just to prove it, we’re going to cover AGI programming in
               a couple of other languages later in the chapter.
               Let’s get started! We’ll look at each section of the code in turn, and describe what it
               does:
                   #!/usr/bin/perl
               This line tells the system that this particular script is written in Perl, so it should use
               the Perl interpreter to execute the script. If you’ve done much Linux or Unix scripting,
               this line should be familiar to you. This line assumes, of course, that your Perl binary
               is located in the /usr/bin/ directory. Change this to match the location of your Perl
               interpreter.
                   use strict;
               use strict tells Perl to act, well, strict about possible programming errors, such as
               undeclared variables. While not absolutely necessary, enabling this will help you avoid
               common programming pitfalls.
                   $|=1;
               This line tells Perl not to buffer its output—in other words, that it should write any
               data immediately, instead of waiting for a block of data before outputting it. You’ll see
               this as a recurring theme throughout the chapter.
                   # Set up some variables
                   my %AGI; my $tests = 0; my $fail = 0; my $pass = 0;
                           You should always use unbuffered output when writing AGI scripts.
                           Otherwise, your AGI may not work as expected, because Asterisk may
                           be waiting for the output of your program, while your program thinks
                           it has sent the output to Asterisk and is waiting for a response.


               Here, we set up four variables. The first is a hash called AGI, which is used to store the
               variables that Asterisk passes to our script at the beginning of the AGI session. The next
               three are scalar values, used to count the total number of tests, the number of failed
               tests, and the number of passed tests, respectively.
                   while(<STDIN>) {
                           chomp;
                           last unless length($_);
                           if (/^agi_(\w+)\:\s+(.*)$/) {
                                   $AGI{$1} = $2;
                           }
                   }


               210 | Chapter 9: The Asterisk Gateway Interface (AGI)
   233   234   235   236   237   238   239   240   241   242   243