Here's a sample program, with annotations.
use CGI qw/:standard/;
$foo = new CGI;
$guest_record = $foo->param('guest_name')
. ":" . $foo->param('email_address') ;
open STDERR, ">&STDOUT"
open GUESTS, ">> guest.data"
or die $foo->header,"Can't open guest database: $!\n";
CGI borrows much of its syntax from UNIX.
- STDIN and GUESTS are file handles.
- STDIN -- the standard source of data input into a program -- is usually the console, what is typed on the keyboard.
- The "less than sign" operator redirects file output "dir.txt" into the mail program.
- STDOUT -- the standard destination for output from the program -- is usually the console.
- This command with the "less than sign" operator below redirects directory listing (ls) output to a file named "dir.txt".
- If "dir.txt" does not exist, it will be created.
If it already exists, it will be overwritten.
- ">>" Double "greater than sign" appends (adds) output to the end of an existing file.
- $! is a Perl built-in variable name for the error message
created when an error occurs.
open(RUN_PARMS, "| get_it");
opens the RUN_PARMS file as input to a command named "get_it".
open(FORMATTED_REPORT_FILE, "format_it |");
opens the FORMATTED_REPORT_FILE to receive output to command named "format_it".
while (<GUESTS>) {
($guest_name, $email_address, $phone_number, $browser) = split /:/;
a loop to split the fields from a PUT statement the GUESTS file.
print %ENV;
prints the names and contents of all environment variables. %ENV is called a "hash",
— an array which associates locations to values using strings instead of numbers.
print "<A href=\"mailto:webmaster\@site.com\">Webmaster</A>. \n";
Special character (such as @ and especially quote marks) must be escaped using the \ character within strings in Perl. Example:
for ($count = 1; $count <= 10; $count += 1) { print("the counter is now $count\n"); }
print GUESTS $guest_record, "\n";
close GUESTS;
"\n" is a line break.
Semi-colons separate commands.
In order to access an SQL database using Perl, there are two pieces required, they are
- DBI module (the DataBase Interface) which enables a CGI program to access a database.
DBD (DataBase Driver) module specific to a database, such as Oracle, Sybase, Informix, and
Hughes Technologies' mSQL (MiniSQL).
downloaded from
http://www.Perl.com/CPAN/ (the Comprehensive Perl Archive Network).
DBI and DBD can be found in category 7 of the archive called Database Interfaces.
- The DBD:ODBC driver generically accesses ODBC, which in turn accesses the databases.