This appendix contains four sections:
A Perl/CGI Overview
Getting Perl
The Shopping Bag Script—bag.pl
The CyberGreeting Script—greet.pl
The first section contains a little background on Perl and mentions some of its advantages. The next section tells you where to download Perl, giving you several choices depending on your operating system. The last two sections explain how the Perl scripts in Chapter 8, and Chapter 10, work.
The acronym stands for Practical Extraction and Report Language. It was originally designed for text and file manipulation, but also does well for managing system tasks and creating dynamic content for the Web. Perl has its roots in programming languages, such as C, sed, awk, and sh.
Perl is popular for a number of reasons. As far as languages go, Perl is pretty easy to learn. It is extremely powerful. It is used in just about every kind of programming scenario imaginable. Here are some of the many ways Perl is used:
For dynamic web page content
In CGI scripts for countless web apps
To access databases
For building search engines and web robots
For password protection and other encryption
For system administration, site logging, and scheduled tasks
For networking tasks and other scripting
For chat servers and message boards
Perl is quickly finding its way into many other arenas. You can use Perl:
To extend Java, C, VisualBasic, Delphi, and other code
In XML (Extensible Markup Language) applications
As PerlScript, an ActiveX scripting engine
Perl is free. You can get it at the CPAN (Comprehensive Perl Archive Network) site http://www.perl.com/CPAN/. Windows users can also get it at the ActiveState site at http://www.activestate.com/.
Perl has a very large and loyal following. Coders are consistently adding to the hundreds, if not thousands, of modules and applications that have been written and that you can easily implement on your web site. The overwhelming majority of this stuff is free. Perl’s popularity means that you’ll also find tons of documentation, support, and experienced coders all over the planet.
Perl runs on lots of platforms, including Unix, VMS, MS-DOS, Windows NT/98/95, OS/2, and more. Most of the code you write is fairly portable from operating system to operating system.
The biggest gripe developers have with Perl is performance. Perl is comparatively slower to execute than compiled languages such as C. In the web environment, CGI scripts (the Common Gateway Interface; more on that shortly) written in Perl (and other languages) must be read from the hard drive and loaded as a new process each time they are called. Technologies such as Active Server Pages and Java servlets can run in the same memory space as the web server, which greatly speeds execution. Developments such as Perl for ISAPI and the more recent PerlEx have increased performance significantly.
Another downside is that Perl is not considered an elegant (looking) language. Perl exchanges beauty for utility. It works, but it can be ugly.
If you use your browser to request a file with an .html extension from a web server, you’re getting a static document. That means that file exists in a directory on the computer. You could sit down at the computer containing the file, open it in a text editor, and view the same data that is being sent to your browser.
If you request a .cgi
file
(or .pl
, .plx
, or other
file extensions), you’re not going to get the code written in
the file. The web server instead executes the file with whatever
engine it is configured to use (in our case, Perl) and returns that
output to the browser. What you’re viewing in your browser
doesn’t exist on the web server. The content was created when
you requested it.
This entire process of requesting and receiving output happens through CGI, the Common Gateway Interface. CGI is a standard for interfacing with HTTP servers. By the way, the server-side language doesn’t have to be Perl. There are plenty of CGI scripts written in C and C++, Python, Fortran, AppleScript, and others. For more details, check out the Perl CGI Programming FAQ at http://www.cpan.org/doc/FAQs/cgi/perl-cgi-faq.html.
Though CGI scripts in general can be outperformed by ColdFusion, Active Server Pages, and other technologies, the CGI standard is still used all over the Net. There are also plenty of web servers out there running Perl. And it’s free. Since this stuff is nearly everywhere, it made sense to write the server-side portion of the JavaScript applications in something that everybody can get their hands on fairly easily. I think you’ll find that these scripts are pretty easy to follow, too.
3.141.31.240