Perl is an interpreted language optimized for scanning arbitrary text files, extracting information from these files, and printing reports based on that information. It is 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. Perl was written by Larry Wall (lwall@sems.com), with the help of lots of other contributors.
But, Why Perl???UNIX system administrators and application developers often have to rely on several different languages to accomplish their tasks. This means learning a number of different syntaxes and having to write in multiple languages to accomplish a task. For example, to process a file, a system administrator might have to write a shell script using sh, process a file using awk or grep, and edit the file using sed. For other uses, the administrator may have to create a C program with its longer create/compile/debug development cycle.
It would be better if the administrator could combine many of these tasks into a simple language that is easy to write and develop, and reasonably efficient and complete. Along comes Perl.
In a single language, Perl combines some of the best features of C, sed, awk, and sh. People familiar with these languages have very little difficulty being productive in Perl. Perl's expression syntax is very C-like. Perl uses sophisticated pattern-matching techniques to scan large amounts of data very quickly. Although optimized for scanning text, Perl can also deal with binary data. If you have a problem on which you would ordinarily use sed, awk, or sh, but it exceeds these tools' capabilities or must run a little faster and you don't want to write the program in a compiled language such as C, Perl may be the language for you.
Perl Plus Points...Perl has many advantages as a general-purpose scripting language. These benefits include its generous licensing (it's free), its interpreted nature, the fact that Perl is available for most platforms, and more.
First, Perl is generally available on most server platforms, including the following:
- Most UNIX variants
- MS-DOS
- Windows NT
- Windows 95
- OS/2
- Macintosh
Perl also has the distinct advantage of being "low cost." It is distributed free of charge or, at most, for a small copying charge. Actually, Perl is distributed under the GNU "copyleft," which means that if you can execute Perl on your system, you should have access to the source of Perl for no additional charge. (Actually, a small copying charge might be imposed.) Perl may also be distributed under the "artistic license," which some people find less threatening than the copyleft.
AvailabilityPerl is readily available from many sources, including any comp.sources.unix archive or CPAN site. If you don't have Perl on your server or development machine, it is easy to obtain either as source code or precompiled binaries for many platforms. For those not on the Internet, Perl is available via anonymous Uucp from both uunet and osu-cis. Perl is often distributed with CD collections of utilities for UNIX platforms.
Interpreted LanguagePerl is interpreted. This can be either an advantage or disadvantage, depending on your needs. For example, Perl has a short development cycle compared to compiled languages, but it will never execute as fast as a compiled language. One advantage of an interpreted language for tool or application development is that you can perform incremental, iterative development and testing without having to go through a create/compile/test/debug/fix cycle. By eliminating the compile portion of the cycle, interpreted languages can speed the development cycle drastically. It can also be helpful if you are evolving your application by implementing it with minimal capabilities and adding advanced capabilities later.
Because it is interpreted and relatively C-like, you can also use Perl as a prototyping language. This can be especially useful with complex or technically difficult projects such as network communication. You can use Perl's shortened development cycle to evaluate your design and then, once it is proven, rewrite the code in the language of your choice. By the way, C and C++ are good choices because Perl is a lot like C and supports much the same functionality.
Easy to UseAbove all, Perl is a language in which you can do things. There are usually several ways to accomplish the same task. Although some techniques are more efficient with system resources than others, users can generally select the technique that is easier for them to use (and maintain/enhance in the future) and go with it.
The ease of use and completeness make Perl appropriate for quick-and-dirty, one-time utilities as well as structured, complex applications.
Efficient
Perl is a straight-line language, which means that simple programs do not have to deal with complex formatting or function/procedure or object/method structures to accomplish their task. As a simple example, let's pay homage to programming texts (including this one) with the "Hello World!" program.
Here it is in C:
void main()
{
printf("Hello World!");
}
And here it is in Perl:
print 'Hello World!'
Get in, get out, and get the job done.
Language CapabilitiesPerl is optimized for text processing and, therefore, is very efficient at many tasks required of system administrators and application developers. Many of the files used in UNIX systems administration are plain text files. Selecting records, processing the selected records, and reporting exceptions are the heart of many tasks performed in UNIX administration.
In the current versions of Perl, the language also includes much additional functionality, making it appropriate for tasks such as processing socket calls, embedding in programs written in C, and maintaining POSIX-compliant systems.
Integration with CPerl can access C libraries to take advantage of much of the code written for this popular language. Utilities included with Perl distributions enable you to convert the headers for these C libraries into their Perl equivalents.
Perl 5.0 can be integrated easily into C and C++ applications. Perl can call or be called by routines written in C or C++. The Perl interface is through a set of perl_call_* functions. The call to C libraries is through the XS language interface.
Relatively Easy to LearnUnlike many programming languages, Perl is designed to be practical rather than beautiful. By this I mean that Perl was designed from the start to be easy to use, efficient, and complete rather than tiny, elegant, and minimal.
Programming in Perl is relatively easy, especially if you have experience in C or another C-like language. Like many scripting languages, Perl reads its programs from the first line to the last line. It doesn't require complex structures to be able to create a program. It does, however, support subroutines or functions and, in version 5.0, can be object oriented.
Perl Has Built-In Debugging FacilitiesThe Perl interpreter has a built-in debugger that can help reduce the time it takes to debug applications. The debugger is activated through the use of the -d switch on the command line. In addition, the -w switch provides a complete set of warnings that can be invaluable in debugging Perl scripts.
Finally, some Salt... Negative PointsPerl has few negatives as a scripting language for system administration tasks and as a language for module development. But there are a few.
Interpreted LanguagePerl is interpreted. Therefore, it will not be as fast as compiled languages such as C or C++. Given the speed of modern CPUs, in all but very large or time-critical applications, this will not make a significant difference. And in fact, the interpreted nature of the language can reduce development time significantly by eliminating the time needed to compile and debug versions of the program.
Perceived as Public Domain
Perl isn't strictly in the public domain (see the license agreement for details). But it's close enough. Many large companies have policies against using public domain or copylefted software. In many cases, this bias is more of a mind-set than a negative, but it can be a detriment to using Perl.
Because Perl is in the public domain, there is no corporation that your company can apply leverage against to get something done. But you do have access to the Perl source to make specific needed changes to your environment, if required.
Informal SupportThe support for Perl is on an informal basis through the volunteer efforts of users worldwide. Does this mean it is bad? No, not necessarily. In fact, the "support" given through the Internet newsgroups is probably as good as any given by a major corporation. But you can't depend on your question being answered, at least in a timely manner. And you don't have a corporation on which you can apply pressure to support your specific environment. On the other hand, you do have access to the source code for Perl and can look into problems yourself.
Protecting Proprietary CodePerl isn't compiled (although there is an effort to make it so). Thus, if you distribute your solutions, you distribute code. This can be a deterrent to producing (at least your final application) in Perl.
Concerns About Reliability
Perl, in its version 5+ incarnation, is undergoing some major changes. Things might not work or might break later. This can be a concern for the future of applications written for a specific version and relying on a specific feature. On the positive side, there are a lot of people testing each release through use. Many of these bugs are quickly detected and ironed out.
Maintainability of Scripts
Perl has somewhat of a reputation for being unreadable. This can be a problem for system maintenance. However, Perl is probably no more unreadable than any C-like language. (C itself, in my opinion, is a very un-pretty-I won't say ugly-language; Perl suffers from that heritage.)
Like with any other language, the maintainability of Perl relies heavily on the willingness of the programmer to structure and comment/document the code. Because many "quick-and-dirty" utilities are written in Perl to get a specific job done and then expanded to be more generally usable, much of the available source code isn't all that pretty. (Sounds a little like the evolution of Perl itself, doesn't it?)
GNU Copyleft License AgreementThe GNU license under which Perl is distributed is really quite innocuous. But, it might be a problem depending upon the type of application you are developing. If you intend to do any of the following, Perl is probably not the best language to use:
Sell the application as a packaged product
Distribute an application that includes trade secrets
Keep your programming techniques secret
What Can Perl Do?Perl is most commonly used to develop system administration tools. But it has also gained enormous popularity on the Internet. Perl can be, and is, used to develop many Internet applications and their supporting utility applications.
UNIX System Maintenance
As mentioned before, Perl can perform the work of several other tools, and usually in less time. It is particularly adept at processing the text files typically used as configuration files.
CGI ScriptsPerl is one of the most popular languages for creating CGI applications. There are literally thousands of examples of dynamic CGI programming in Perl. Perl can be used to create dynamic Web pages that can change depending on factors such as which visitor is viewing them.
One of the most common uses of Perl on the Internet is to process form input. Perl is especially adept at this chore because most of that input is textual-Perl's strength.
Mail ProcessingAnother popular use of Perl is for the automated processing of Internet e-mail. Perl scripts have been used to filter mail based on address or content. Perl scripts have also been written to automate mailing lists. One of the most popular of these programs is Majordomo.
Automating Web Site MaintenancePerl can be used to automate the maintenance of Web sites. Because Web pages are little more than text files in a specific format, Perl is particularly adept at processing them. Perl's socket capability can also be used to contact other sites and request information using HTTP. There has even been a Web server written in Perl.
In order to check the links on a site, a Perl program must parse the sites pages starting with the main page, extract the URLs, and determine whether these URLs are still active.
Automating File Retrieval
There are several FTP clients written in Perl. Perl can be used to automate file retrieval via FTP. Again, this combines the socket capability of Perl with its text-processing capability.
Is Perl for Me?Only you can answer this question! :)