Perl: The Complete Referenceby Martin C. Brown
Master Perl - the multi-platform, flexible scripting language - with help from this reference. Programming expert Martin Brown shows you how to write portable Perl scripts that can be used to report on text files, handle complex data structures, enhance the user interface, program for the Web, and much more. Plus, you'll learn to deploy Perl programs across all… See more details below
Master Perl - the multi-platform, flexible scripting language - with help from this reference. Programming expert Martin Brown shows you how to write portable Perl scripts that can be used to report on text files, handle complex data structures, enhance the user interface, program for the Web, and much more. Plus, you'll learn to deploy Perl programs across all platforms.
Read an Excerpt
Chapter 1: Perl BackgrounderMain Perl Features
Perl contains many features that most Perl programmers do not even know about, let alone use. Some of the most basic features are described here.
Perl Is Free
It may not seem like a major feature, but, in fact, being free is very important. Some languages, such as C (which is free with compilers such as GNU's gcc), have been commercialized by Metrowerks, Microsoft, and other companies. Other languages, such as Visual Basic, are entirely commercial. Perl's source code is open and freeanybody can download the C source that constitutes a Perl interpreter. Furthermore, you can easily extend the core functionality of Perl both within the realms of the interpreted language and by modifying the Perl source code.
Perl Is Simple to Learn, Concise, and Easy to Read
Because of its history and roots, most people with any programming experience will be able to program with Perl. It has a syntax similar to C and shell script, among others, but with a less restrictive format. Most programs are quicker to write in Perl because of its use of built-in functions and a huge standard and contributed library. Most programs are also quicker to execute than other languages because of Perl's internal architecture (see the section, "Perl is Fast" that follows). Perl can be easy to read, because the code can be written in a clear and concise format that almost reads like an English sentence. Unfortunately, Perl also has a bad habit of looking a bit like line noise to uninitiated. Whether or not your Perl looks good and clean really depends on how you format it-good Perl is easy read. It is also worth reading the Perlstyle guidelines (in the Perl style manual page that comes with Perl) to see how Larry Wall, Perl's creator, likes things done.
Perl Is Fast
As we will see shortly, Perl is not an interpreter in the strictest sense-when you execute a Perl program it is actually compiled into a highly optimized language before it is executed. Compared to most scripting languages, this makes execution almost as fast as compiled C code. But, because the code is still interpreted, there is no compilation process, and applications can be written and edited much faster than with other languages, without any of the performance problems normally associated with an interpreted language.
Perl Is Extensible
You can write Perl-based packages and modules that extend the functionality of the language. You can also call external C code directly from Perl to extend the functionality further. The reverse is also true: the Perl interpreter can be incorporated directly into many languages, including C. This allows your C programs to use the functionality of the Perl interpreter without calling an external program.
Perl Has Flexible Data Types
You can create simple variables that contain text or numbers, and Perl will treat the variable data accordingly at the time it is used. This means that unlike C, you don't have to worry about converting text and numbers, and you can embed and merge strings without requiring external functions to concatenate or combine the results. You can also handle arrays of values as simple lists, as typical indexed arrays, and even as stacks of information. You can also create associative arrays (otherwise known as hashes) which allow you to refer to the items in the array by a unique string, rather than a simple number. Finally, Perl also supports references, and through references objects. References allow you to create complex data structures made up of a combination of hashes, lists and scalars.
Perl Is Object Oriented
Perl supports all of the object-oriented features-inheritance, polymorphism, and encapsulation. There are no restrictions on when or where you make use of objectoriented features. There is no boundary as there is with C and C++.
Perl Is Collaborative
There is a huge network of Perl programmers worldwide. Most programmers supply, and use, the modules and scripts available via CPAN, the Comprehensive Perl Archive Network (see Web Appendix B at www.osborne.com). This is a repository of the best modules and scripts available. Using an existing prewritten module can save you hundreds, perhaps even thousands, of hours of development time.
Compiler or Interpreter
Different languages work in different ways; they are either compiled or interpreted. A program in a compiled language is translated from the original source into a platformspecific machine code. This machine code is referred to as an executable. There is no direct relation between the machine code and the original source: it is not possible to reverse the compilation process and produce the source code. This means that the compiled executable is safe from intellectual property piracy.
With an interpreted language, on the other hand, the interpreter reads the original source code and interprets each of the statements in order to perform the different operations. The source code is therefore executed at run time. This has some advantages: Because there is no compilation process, the development of interpreted code should be significantly quicker. Interpreted code also tends to be smaller and easier to distribute. The disadvantages are that the original source must be supplied in order to execute the program, and an interpreted program is generally slower than a compiled executable because of the way the code is executed.
Perl fits neither of these descriptions in the real sense. The internals of Perl are such that at the time of executing a Perl script, the individual elements of the script are compiled into a tree of opcodes. Opcodes are similar in concept to machine code-the binary format required by the processor in your machine. However, whereas machine code is executed directly by hardware, opcodes are executed by a Perl virtual machine. The opcodes are highly optimized objects designed to perform a specific function. When the script is executed you are essentially executing compiled C code, translated from the Perl source. This enables Perl to provide all the advantages of a scripting language while offering the fast execution of a compiled program. This mode of operation-translation and then execution by a virtual machine is actually how most modern scripting languages work, including Java (using Just In Time technology) and Python.
Keeping all of that in mind, however, there have been some advances in the most recent versions of a Perl compiler that takes native Perl scripts and converts them into directly executable machine code. We'll cover the compiler and Perl internals later in this book.
Similar Programming Languages
We already know that Perl has its history in a number of different languages. It shares several features and abilities with many of the standard tools supplied with any Unix workstation. It also shares some features and abilities with many related languages, even if it doesn't necessarily share the same heritage.
With regard to specific features, abilities, and performance, Perl compares favorably against some languages and less favorably against others. A lot of the advantages and disadvantages are a matter of personal preference. For example, for text handling, there is very little to choose between awk and Perl. However, personally I prefer Perl for those tasks that involve file handling directly within the code, and awk when using it as a filter as part of a shell script.
Any of the Unix shells-sh, csh, ksh, or even bash-share the same basic set of facilities. They are particularly good at running external programs and at most forms of file management where the shell's ability to work directly with many of the standard Unix utilities enables rapid development of systems management tools.
However, where most shells fail is in their variable- and data-handling routines. In nearly all cases you need to use the facilities provided by shell tools such as cut, paste, and sort to achieve the same level of functionality as that provided natively by Perl...
and post it to your social network
Most Helpful Customer Reviews
See all customer reviews >