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.