What Is Perl?

A large variety of programming languages exists for Linux, ranging from simple shell scripting variants such as csh and bash, to high-level interpreted languages, such as Perl, Tcl, and Python, to the most hardcore of them all, C and C++. Naturally, each has its own advantages and disadvantages, which you can see in more detail at The Great Computer Language Shootout, at www.bagley.org/~doug/shootout/ .

Perl bridges the gap nicely between the two extremes, the simple and the hardcore, by supporting some of the best of both breeds features. For example, Perl allows you to write applications that interact with the shell, execute commands, and process the output with the same ease of use and rapid development that is seen with shell scripting. You can use a simple editor to write your program and then run it without worrying about linking and compiling it, which is the case with the advanced compiled languages such as C and C++. On the other hand, Perl also supports advanced programming constructs, complex data structures, and a higher level of security, as found in high-level compiled languages, but commonly lacking in scripting languages.

Let s look at a hypothetical example that illustrates these points better. Say you decide to develop an application that needs to monitor hard disk partitions and alert an administrator if the amount of available space drops below 10 percent. The application must also determine which users are occupying the most space and send them an e-mail urging them to clean up by listing their 15 largest files. What would happen if you were to try to develop this using a shell scripting language, such as bash? First, you would start out using the df command, which returns a report of the disk space usage. The report could be something like the following, depending on how your machine is partitioned:

 Filesystem           1K-blocks      Used Available Use% Mounted on /dev/hda1              3020140   1582492   1284232  56% / /dev/hda5              7684844   1280812   6013656  18% /data /dev/hda2              3020172   2637411    229341  92% /home none                     94868         0     94868   0% /dev/shm 

You would then need to extract the percentage values from this report, which is more difficult than it seems; most shell scripting languages don t support this type of text manipulation directly. As a result, you would need to use a language such as sed or awk to get at the data that you are looking for. Both sed and awk are languages designed specifically for pattern matching and transformation and are used extensively in shell scripting. Once you determine that the /dev/hda2 partition (the /home filesystem) exceeds the 90 percentage usage threshold, you would need to calculate each user s individual usage and determine their largest files. How would you do that?

You would need to get a list of all the users home directories in the filesystem, most likely using the ls command, and then iterate through each one using a loop of some sort . As you go through each directory, you would issue the du command to get a list of all the files and their sizes, like so:

 3656    /home/postgres/base/1 3656    /home/postgres/base/16975 4       /home/postgres/base/16976/pgsql_tmp 1128708 /home/postgres/base/16976 1136024 /home/postgres/base 140     /home/postgres/global 82024   /home/postgres/pg_xlog 12      /home/postgres/pg_clog 1218232 /home/postgres 

The most difficult part of this process is to keep track of each user s usage and his or her files, both the name and the size . Why is this difficult? Shell scripting languages have very primitive support for complex logic operations and data structures. You would need to come up with an alternative mechanism to temporarily store this individual data, and then process and sort it once you finish going through all the users directories. The final part of the application would involve sending an e-mail, both to the offending users and the administrator, using the mail or sendmail commands.

You may have noticed that we glossed over the section regarding sed and awk here. There s a reason for this ”shell scripting languages allow you to develop scripts that invoke shell commands easily and without much effort. In addition, the development cycle is rather simple; you use an editor to modify and debug the script and then simply execute it from the command line. On the other hand, shell scripts fall apart when you need to implement any type of complex logical operations or store relational or structured data because shell scripting languages were not designed for this purpose.

On the flip side, how would you fare if you developed this application using Perl? You would retain some of the advantages of shell scripting, but also have access to more advanced language features. Because Perl allows you to interact with the shell, you can use the df command to get the disk usage report, and then use pattern-matching expressions to easily parse and extract the percentage values. If you find a partition that exceeds the threshold value, you can use the internal readdir function, along with a for loop, to iterate through all of the main directories on that filesystem. Perl integrates well with the underlying operating system to abstract certain functions on all supported platforms. This allows you to use a function such as readdir to get a list of all the files in a particular directory, whether you are using Linux or Mac OS X.

For each directory, you can use the df command, as you did before, to get a list of all the individual files and their sizes and then store them in a complex data structure. Once you finish iterating through all the directories, you can use Perl s sort function to sort and order the information in the data structure to determine the offending users and their largest files. At that point, you can send an e-mail to the administrator and the necessary users, either using the external mail or sendmail commands, or a Perl extension such as Mail::Send . Typically, you would want to use an extension, instead of an external shell command, because it is more efficient and allows you to better implement the desired functionality. You ll actually see how to write this application in Perl later in the chapter, after looking through some simpler examples first.

Detailing the advantages or disadvantages of developing a similar application in a compiled language, such as C or C++, is beyond the scope of this chapter. However, if you are interested in a high-level overview, a language such as C provides more control (memory management) and typically better performance, but at the expense of development time and difficulty.

The next sections take a more in-depth look at the advantages and disadvantages of Perl. This will allow you to evaluate for yourself what Perl has to offer.


As previously discussed, Perl provides a good mixture of features and performance that lies somewhere between a scripting language and a compiled language. To understand this better, here are some specific advantages of Perl:

  • Perl provides great flexibility in developing applications. Perl does not force you to use any specific style or paradigm, but instead allows you to express your ideas and thoughts in the way that you choose. In fact, one of Perl s mottos is , There is more than one way to do it.

  • Perl is extensible. You can write modules or libraries in Perl to extend Perl s functionality and make them available for reuse in other projects. The CPAN holds hundreds and thousands of freely available extensions to interface and communicate with various utilities, applications, Internet services, and database engines.

  • Perl is highly portable and runs on a large number of hardware and software platforms, including most known UNIX/Linux platforms, Windows, Mac OS, OS/2, VMS, and even Palm. Programs that don t use platform-specific functionality, such as pipes in UNIX, or OLE/COM in Windows, will work unchanged across platforms.

  • Perl supports powerful regular expressions for pattern matching. You can use these expressions to search for patterns or specific strings in a body of text.

  • Perl has a strong developer community, which is very active in implementing extensions, fixing bugs , and providing technical support.

  • Perl is free for both commercial and non-commercial use.


Like any other programming language, Perl does have its share of disadvantages:

  • The flexibility that Perl provides can sometimes mesmerize developers into producing hard-to-maintain code. Because there are so many different ways to solve the same problem, it s easy to come up with a solution that no one else can follow, especially if your documentation isn t up to snuff.

  • Perl has weak exception handling abilities that make it difficult for you to handle any errors that crop up during the execution of your script ”for example, if a file isn t where it s supposed to be, or if write-access to one of your disks isn t permitted.

However, for the most part, Perl is perfectly capable of doing the tasks required of it in a sensible and organized way.

Beginning Fedora 2
Beginning Fedora 2
ISBN: 0764569961
EAN: 2147483647
Year: 2006
Pages: 170

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