Perl in FreeBSD


Although a concerted effort has been made to eliminate them in favor of shell scripts or compiled programs, some of the programs that make up FreeBSD's core installation are actually Perl scripts. The rest are generally compiled binaries, built from source code written in C, C++, or a number of other popular languages. These binaries cannot be read by a human or easily decompiled back to source form. Perl code, on the other hand, does not need to be compiled. It exists as plain text, easily readable and editable, and is run with the help of the Perl interpreter, /usr/local/bin/perl.

Note

Until FreeBSD 5.0, Perl was included by default in the core operating system. For a variety of reasons, it is no longer in /usr/bin as part of a newly installed system and must be installed from the ports (/usr/ports/lang/perl5) or as a package (pkg_add -r perl5).


Chapter 10 described how to write scripts using the shell interpreter /bin/sh by specifying the name of the interpreter on the first line of the script. Perl scripts work the same way. The difference is that whereas shell scripts are primarily useful as "batch programs" (in MS-DOS parlance) that execute sequences of system commands as they would be entered on the command line with simple variable substitution and flow control, Perl is a fully functional programming environment that allows you to write programs as complex as web servers or database-management systems. Because they're interpreted rather than compiled, Perl programs tend to be slower in execution (especially to start) than their C counterparts, although C programs typically take longer to develop properly. Perl's primary strengths are in string processing and text handlingfeatures that are far more difficult to work with in C. Therefore the ideal application for a Perl program is as a system-utility script (such as adduser, which until recently was a Perl script) or a CGI program that performs string manipulations and operations on files. Perl can be less efficient at doing heavy-duty mathematics than a well-designed C or C++ program. For this reason, some Perl modules (such as the MD5 hashing module) contain core mathematical components written in C for speed.

Although the parts of FreeBSD itself that used to be Perl-based are not so any longer, Perl is still widely found in third-party applications such as Majordomo, the popular mailing list manager, and SpamAssassin, the mail filtering suite. In a live FreeBSD system, you will probably run into Perl programs at least as frequently as you will shell scripts, so it's important to know how to work with them.

Strengths of Perl

Perl strikes a balance between the ease of use of shell scripting (a high-level approach) and the versatility of C programming (a low-level approach). The result is a language that allows you to use variables of many different data typesincluding data structures, importable objects, associative arrays, multidimensional arrays, and I/O handleswithout having to worry about casting variables from one type to another, allocating and freeing memory, predeclaring variables, dereferencing pointers, prototyping functions, and performing a number of the other onerous tasks associated with programming in C, including compiling binaries.

Perl gives you extreme freedom to do what you like without getting bogged down in the convoluted and bug-prone details of low-level programming. The high-level tools that make Perl what it is are largely responsible for Perl's vast acceptance as the language of choice for CGI programming and server-side web applications, a field that has grown explosively over the years due to the increase in e-commerce.

The specific purpose of Perl is to provide superior string-manipulation tools: pattern matching, string replacement and translation, regular expressions, and a structure that makes it very straightforward to read and write text files using native arrays of strings. There is no need in Perl to declare a variable as being a string, an integer, a character, or any other type. Perl recognizes variable types in a cascading structure of appropriateness, assigning the necessary memory to each variable as it is modified and applying operators appropriately according to the variable's type.

In Perl we have what is arguably the best mixture of ease of use and programming power in any widely used language. Its syntax is extremely lax and flexible, letting you get away with many minor mistakes that other languages would punish severely. It's extensible (with thousands of importable library modules available throughout the Internet), it's well-documented and well-supported, and it's available everywhereon dozens of different platforms. You can be sure that a Perl script will operate the same no matter where you run itwhether it's on Linux, Mac OS X, IRIX, Windows, or FreeBSD.

Weaknesses of Perl

Perl was never designed to be the general-purpose language it has become. It started life as a string-processing language, and over the years more and more functionality has been heaped upon it. The result is that text-processing remains extremely fast and efficient in Perl, but it does so at the expense of structural elegance. Although Perl's strengths are very significant assets indeed, Perl is not widely regarded as a model of ideal language design.

Although Perl's laxness lets the programmer get away with many types of mistakes, this has the effect of encouraging sloppy code (much as a standards-lax web browser will encourage invalid or incompatible HTML code). Perl accepts syntax that resembles C, BASIC, Fortran, or even common English in some cases. Although this gives the language great flexibility, it also means that Perl code that's perfectly natural to one programmer might be unreadable to another. Perl's structure allows many different ways to do any given task; this results in a learning curve that grows much steeper after the initial easy introduction to the language, and it means that unless certain coding practices are enforced, Perl programs can be a severe challenge for multiple programmers to maintain. Perl is in many ways the "black sheep" of the programming language community.

Python and Post-Perl Programming

Many new projects have attempted to fill in for Perl's shortcomings since its meteoric rise to prevalence. Many, such as REBOL, Java, and Ruby, have been under development since Perl gained critical mass in the server-programming world. The frontrunner, however, is a language dating back to about the same time as Perl's beginnings: Python.

Python is an even more object-oriented language than Perl, and it has a much more regimented syntax. No C-style brackets are used; instead, tab indentations are significant, greatly simplifying the hierarchical nesting of flow-control blocks. Python also uses dotted-object hierarchies and tightly structured modules (or dictionaries), providing a much more direct and extensible interface to objects than Perl can.

Whereas Perl programs are compiled with each runtime, Python generates compiled bytecode (a .pyc file) at the first run of a program, which speeds up execution on subsequent occasions. This is partially necessary because, as with all object-oriented programming environments, the trade-off is speed for structure. Because of its strict object-oriented nature, in which syntactic structure and a single method of doing things are paramount to its design, Python programs are potentially less susceptible to bugs than is Perl. Python also is easier to maintain because it is less prone to "spaghetti code." Python lacks many text-processing capabilities, though, which is the area where Perl shines the most.

Although Python is not a part of the core FreeBSD system, it is available from the ports collection, in /usr/ports/lang/python. Its proponents continue to gain strength and credibility, and Python may eclipse Perl as the dominant interpreted programming language before too much longer.





FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net