Archives
What's New
Site Map
Subscriptions

Home
NetFlash
This Week
Forums
Reviews/buyer's guides
Net Resources
Industry/Stocks
Careers
Seminars and Events
Product Demos/Evals
Audio Primers

IntraNet


























For more info:

Perl Language Home Page - Download Perl, read updates, sign up for mailing lists and training.

Perl for Win32 platforms

comp.lang. perl - Newsgroup to discuss Perl, ask questions, etc.

Contact Contact Mark Gibbs


Perl: Putting practicality before beauty

By Mark Gibbs
IntraNet, 3/24/97

When it comes to creating system-level applications in general, and Web server scripts in particular, the Perl programming language stands out with its maturity and power.

Perl, an acronym for Practical Extraction and Reporting Language (or as Perl's creator and other aficionados sometimes claim: Pathologically Eclectic Rubbish Lister), is one of the most powerful and popular programming languages.

Perl dates back to 1987, when one man, Larry Wall, began developing the language simply as a tool for solving the systems programming problems he encountered as a Unix administrator. Despite its humble beginnings, Perl has grown into a richly featured, complex language.

Perl is appealing because it fills the gap between Unix shell programming and C applications. Perl offers much of the simplicity of the former and the functionality of the latter.

As described by Wall: 'Perl is an interpreted language optimized for scanning arbitrary text files, extracting information from those text files and printing reports based on that information. It's also a good language for many system management tasks. The language is intended to be practical (easy to use, efficient and complete) rather than beautiful (tiny, elegant and minimal).'

Wall also points out that Perl's expression syntax corresponds quite closely to C expression syntax; Perl does not arbitrarily limit the size of your data - 'if you've got the memory, Perl can slurp in your whole file as a single string;' recursion is of unlimited depth; and Perl uses sophisticated pattern-matching techniques to scan large amounts of data quickly.

Perl, which uses straightforward syntax, data types and structures, is fairly easy to learn. By the same token, well-written Perl programs are usually easy to read and understand.

But that apparent simplicity belies Perl's rich feature set, which includes a built-in symbolic debugger and support for object-oriented programming. A sophisticated data-flow tracing mechanism lets Perl determine if data comes from unsecure sources so potentially dangerous operations can be prevented.

Perl is highly portable, runs on all major and many minor platforms, can access TCP/IP sockets and can be integrated with C. It also offers many specialized extensions for services such as database and X.500 directory access. Topping off the list of benefits are the newsgroups and Web sites galore that provide support and resources for Perl programmers. To an intranet manager, this is implementation nirvana: few constraints, amazing flexibility and lots of support.

As if all this isn't enough, Perl is distributed under the GNU Copyleft license (see www.gnu. org). This means use of Perl is free.

The Perl whirl

Perl scripts or programs, are plain text files converted by the Perl run-time into a byte-code version that allows for error detection and data-flow tracing. The Perl interpreter then converts the byte-code representation to machine code before execution.

As mentioned above, Perl is fairly easy to learn. Most Perl statements end with a semicolon, and many of the statements look like BASIC or C code. For example:


     # My first program.

     print 'Hello Word. \n';
is, of course, the industry standard first program. It prints to the console. The above code, which we'll store in a file named 'hello.pl,' is actually a complete Perl script. To run it, we need to invoke the Perl interpreter, thus:

     Perl hello.pl
We can get the same result by entering the following command line:

Perl -e 'print 'Hello Word. \n';'
The 'e' flag allows you to execute Perl statements from the command line. Perl offers a number of command-line flags that can control processing options and development features such as the enablement of the debug mode.

Perl is most commonly used as the language for creating back-end applications for Web servers. In the following example, you can see some of the attributes of Perl in a simple application. The application will accept data from an HTML form and return the form data to the browser as an HTML page. (The line numbering isn't part of Perl; it's for reference.)


1 # A (very) simple Web server script.

2 #

3 #!/usr/local/bin/Perl

4 if ($ENV{'REQUEST-METHOD'} eq 'GET') { 

5 $FORM-DATA =  

  $ENV{'QUERY-STRING'};

6   } else {

7   $LENGTH = $ENV  

   {'CONTENT-LENGTH'};

8   while ($LENGTH) { 

9        $FORM-DATA .= getc(STDIN);

10          $LENGTH-;

11        }

12    }

13 print 'Content-type: text/html\n\n';

14 print 'REQUEST-METHOD: $ENV{'REQUEST-METHOD'}<BR>\n';

15 print 'CONTENT-LENGTH: 

$ENV{'CONTENT-LENGTH'}<BR>\n';

16 print 'QUERY-STRING: 

$ENV{'QUERY-STRING'}<BR>\n';

17 print '<HR>\n';

18 print 'FORM-DATA: $FORM-DATA\n';
The first two lines are comments, as denoted by the hash symbols. The third line is specific to Unix environments in which the script is invoked by its name only - the Unix shell needs to be told where to find the Perl interpreter. The '#!' construct tells the operating system where to find the executable to handle this script.

The fourth line shows off one of Perl's internal variables. '$ENV' lets you retrieve the value of environment variables. Perl has an extensive range of these special variables.

'$FORM-DATA' (first used on Line 5) is a user-defined variable. If the method used to pass data to the Web application is 'GET,' the argument is retrieved from the environment variable named 'QUERY-STRING' (constructed on the fly by the Web server) and stored in '$FORM-DATA' (again on Line 5).

If the method is not 'GET,' then we assume it is 'POST' and get the data from the Web application's standard input. First, the number of characters to read is retrieved from 'CONTENT- LENGTH,' another Web-server-created environment variable (see Line 7).

Then we use a while loop for that number of characters and Perl's 'getc' function to read the data from the standard input into the '$FORM-DATA' variable (Lines 8 through 11). Perl also supports all of the other expected control structures such as until, do-while, do-until, and unless.

For simplicity's sake, in this example we didn't check if 'CONTENT- LENGTH' is less than some expected maximum number of characters, but we would in actuality. While a ridiculous number of characters is unlikely to cause Perl to behave in an unexpected way, the load on the server from trying to read, say, one billion characters, could cause performance problems.

All of the output, including the partial HTTP header (Line 13), is generated by printing to the standard output.

While this is a trivial Web server application, it is fully functional and could be used as the skeleton for a far more sophisticated purpose, such as an HTML form handler. That's the beauty of Perl: It can handle the smallest of problems, as well as effectively tackle the big ones.

Perl is steadily evolving. Implementations are available for a tremendous range of platforms such as Application System/400, Macintosh, MS DOS, MVS, NetWare, OS/2, Unix, VMS and Windows 3.1, 95 and NT, as well as support for Microsoft Corp.'s Internet Server API. A compiler is in alpha release. Perl is a uniquely rich programming environment with an enthusiastic following and a lot of supporting software. It is a prime tool for intranets.


Feedback | Network World, Inc. | Sponsor index
How to Advertise | Copyright

Home | NetFlash | This Week | Industry/Stocks
Buyer's Guides/Tests | Net Resources | Opinions | Careers
Seminars & Events | Product Demos/Info
Audio Primers | IntraNet